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

55 Answered Questions

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

28 Answered Questions

7 Answered Questions

[SOLVED] How to use Regular Expressions (Regex) in Microsoft Excel both in-cell and loops

  • 2014-03-20 19:09:13
  • Portland Runner
  • 744844 View
  • 506 Score
  • 7 Answer
  • Tags:   regex excel vba

9 Answered Questions

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

18 Answered Questions

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

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

16 Answered Questions

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

  • 2009-01-11 07:21:20
  • nickf
  • 668647 View
  • 1196 Score
  • 16 Answer
  • Tags:   javascript regex

11 Answered Questions

[SOLVED] How to do a regular expression replace in MySQL?

10 Answered Questions

[SOLVED] How to match "anything up until this sequence of characters" in a regular expression?

  • 2011-08-19 16:45:44
  • callum
  • 521388 View
  • 405 Score
  • 10 Answer
  • Tags:   regex

17 Answered Questions

[SOLVED] Regex for numbers only

  • 2008-11-07 18:42:49
  • Timothy Carter
  • 678586 View
  • 267 Score
  • 17 Answer
  • Tags:   c# regex .net-2.0

5 Answered Questions

[SOLVED] \d is less efficient than [0-9]

Sponsored Content