By Jake Pearson


2011-06-09 20:03:09 8 Comments

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.

3 comments

@matchew 2011-06-09 20:26:02

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

@DJClayworth 2014-08-08 15:30:41

@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]+$/

@Prusse 2011-06-09 20:08:46

"^" 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

@Howard 2011-06-09 20:04:31

Use the start and end delimiters: ^abc$

@matchew 2011-06-09 20:27:40

@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'

@Jake Pearson 2011-06-09 20:34:39

This worked for me, maybe my example should have been "abc def ghi" as the match target.

@matchew 2011-06-09 20:41:18

@Jake if your string was "abc def ghi" then /^abc$/ would not work. ex: jsfiddle.net/XUyAc

@Jake Pearson 2011-06-09 20:45:46

I get that, if I want to match "abc def ghi" my regex would be ^abc def ghi$

@matchew 2011-06-09 20:50:39

yes, that would be correct.

@Wiktor Stribi┼╝ew 2017-08-17 11:01:03

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.

Related Questions

Sponsored Content

46 Answered Questions

[SOLVED] How to replace all occurrences of a string in JavaScript

15 Answered Questions

[SOLVED] How do you access the matched groups in a JavaScript regular expression?

  • 2009-01-11 07:21:20
  • nickf
  • 628169 View
  • 1129 Score
  • 15 Answer
  • Tags:   javascript regex

48 Answered Questions

19 Answered Questions

[SOLVED] Is it possible to apply CSS to half of a character?

52 Answered Questions

[SOLVED] Create GUID / UUID in JavaScript?

  • 2008-09-19 20:01:00
  • Jason Cohen
  • 1405111 View
  • 3518 Score
  • 52 Answer
  • Tags:   javascript guid uuid

27 Answered Questions

18 Answered Questions

[SOLVED] How do you use a variable in a regular expression?

  • 2009-01-30 00:11:05
  • JC Grubbs
  • 577889 View
  • 1079 Score
  • 18 Answer
  • Tags:   javascript regex

29 Answered Questions

[SOLVED] Is JavaScript a pass-by-reference or pass-by-value language?

31 Answered Questions

[SOLVED] endsWith in JavaScript

6 Answered Questions

[SOLVED] Check whether a string matches a regex in JS

Sponsored Content