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.


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

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)

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

Live example:

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:

@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

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

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

@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

8 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
  • 823896 View
  • 552 Score
  • 8 Answer
  • Tags:   regex excel vba

19 Answered Questions

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

  • 2009-01-30 00:11:05
  • JC Grubbs
  • 686592 View
  • 1256 Score
  • 19 Answer
  • Tags:   javascript regex

59 Answered Questions

[SOLVED] How to replace all occurrences of a string?

12 Answered Questions

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

  • 2009-06-12 14:08:42
  • Piskvor cc-by-sa 3.0
  • 420821 View
  • 484 Score
  • 12 Answer
  • Tags:   mysql regex mysql-udf

29 Answered Questions

[SOLVED] Regular expression to match a line that doesn't contain a word

17 Answered Questions

[SOLVED] Regex for numbers only

  • 2008-11-07 18:42:49
  • Timothy Carter
  • 735443 View
  • 289 Score
  • 17 Answer
  • Tags:   c# regex

18 Answered Questions

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

  • 2009-01-11 07:21:20
  • nickf
  • 722078 View
  • 1279 Score
  • 18 Answer
  • Tags:   javascript regex

9 Answered Questions

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

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
  • 593475 View
  • 455 Score
  • 10 Answer
  • Tags:   regex

5 Answered Questions

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

Sponsored Content