231

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.

LWC
  • 1,084
  • 1
  • 10
  • 28
Jake Pearson
  • 27,069
  • 12
  • 75
  • 95
  • 8
    For those new to regex, there are two good answers to this, depending on what you're looking for. The asker really wants `\babc\b`, which would allow for e.g. `123 abc 123` to match for `abc` (but not the negative examples like in the question); however, `^abc$` will make sure that `a` is at the beginning of the string and `c` is at the end - otherwise it won't match. – Andrew Sep 08 '17 at 13:58

3 Answers3

421

Use the start and end delimiters: ^abc$

Howard
  • 38,639
  • 9
  • 64
  • 83
  • 3
    @Jake, I'm glad howards answer worked, but I think you should note it will only work when only abc is the only item in the string. For example, It would not match 'the first 3 letters in the alphabet are abc' – matchew Jun 09 '11 at 20:27
  • This worked for me, maybe my example should have been "abc def ghi" as the match target. – Jake Pearson Jun 09 '11 at 20:34
  • 1
    @Jake if your string was "abc def ghi" then /^abc$/ would not work. ex: http://jsfiddle.net/XUyAc/ – matchew Jun 09 '11 at 20:41
  • 3
    I get that, if I want to match "abc def ghi" my regex would be `^abc def ghi$` – Jake Pearson Jun 09 '11 at 20:45
  • 6
    Note: to make a pattern with alternations match a whole string, it might be necessary to wrap it with a (non)capturing group: `/^(?:abc|def)$/` or `/^(abc|def)$/`. Otherwise, if the group is not used, `/^abc|def$/` will match `abc` at the start of the string OR `def` at the end of the string. – Wiktor Stribiżew Aug 17 '17 at 11:01
  • @JakePearson comment's saved my sanity; now I got all. – Marcelo Scofano Diniz Sep 18 '20 at 18:11
  • @WiktorStribiżew Thank you for your comment, this is exactly what happened to me and I had no idea what is going on. – user1328889 Apr 09 '21 at 08:17
  • @user1328889 There are specific answers related to this problem, e.g. [Matching several alternatives as whole strings only using pipe OR operator](https://stackoverflow.com/a/48661648/3832970). – Wiktor Stribiżew Apr 09 '21 at 08:32
  • @WiktorStribiżew point is right, if I have this string `"78,10072,10118,78,10072,10078,10088,10078,78"` and want to search for all the repetition of `"78"`, then this regexp: `"^78(,|$)"` will give me the first occurrence only. the answer works when you have not the search value multiple times in your string. – Mohamad Eghlima Apr 22 '21 at 23:05
60

It depends. You could

string.match(/^abc$/)

But that would not match the following string: 'the first 3 letters of the alphabet are abc. not abc123'

I think you would want to use \b (word boundaries):

var str = 'the first 3 letters of the alphabet are abc. not abc123';
var pat = /\b(abc)\b/g;
console.log(str.match(pat));

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:

var strs = ['abc', 'abc1', 'abc2']
for (var i = 0; i < strs.length; i++) {
    if (strs[i] == 'abc') {
        //do something 
    }
    else {
        //do something else
    }
}

While you could use

if (str[i].match(/^abc$/g)) {
    //do something 
}

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 regexes: https://developer.mozilla.org/en/JavaScript/Guide/Regular_Expressions

Edric
  • 24,639
  • 13
  • 81
  • 91
matchew
  • 19,195
  • 5
  • 44
  • 48
  • 13
    @NiharSawant It's because this isn't the answer to the question the OP asked. The OP clearly *doesn't* want to match "'the first 3 letters of the alphabet are abc", and the second solution here does not work for matching general regexes, e.g. /^[abc]+$/ – DJClayworth Aug 08 '14 at 15:30
16

"^" For the begining of the line "$" for the end of it. Eg.:

var re = /^abc$/;

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

Prusse
  • 4,287
  • 2
  • 24
  • 29