What is the regular expression (in JavaScript if it matters) to only match if the text is an exact match? That is, there should be no extra characters at other end of the string.
For example, if I'm trying to match for abc
, then 1abc1
, 1abc
, and abc1
would not match.
"^" For the begining of the line "$" for the end of it. Eg.:
Would match "abc" but not "1abc" or "abc1". You can learn more at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
Use the start and end delimiters:
^abc$
It depends. You could
But that would not match the following string: 'the first 3 letters of the alphabet are abc. not abc123'
I think you want to use \b (word boundaries)
Live example: http://jsfiddle.net/uu5VJ/
If the former solution works for you, I would advise against using it.
That means you may have something like the following:
While you could use
It would be considerably more resource intensive. For me, a general rule of thumb is for a simple string comparison use a conditional expression, for a more dynamic pattern use a regular expression.
more on JavaScript regex's: https://developer.mozilla.org/en/JavaScript/Guide/Regular_Expressions