javascript regex of a javascript string

I need to match a javascript string, with a regular expression, that is a string enclosed by single quote and can only contain a backslashed single quote.

The examples string that i would match are like the following:




This is the regex that matches all valid JavaScript literal string (that is surrounded by single quote ') and reject all invalid ones. Note that strict mode is assumed.


Or a shorter version:


The regex above is based on the definition of StringLiteral (ignoring the double quoted version) specified in ECMAScript Language Specification, 5.1 Edition published in June 2011.

The regex for the JavaScript literal string surrounded with double quote " is almost the same:


Let's dissect the monster (the longer version, since it is direct translation from the grammar):

  • A StringLiteral (ignoring the double quote version) starts and ends with ', as it can be seen in the regex. In between the quotes is an optional sequence of SingleStringCharacter. This explains the * - 0 or more characters.

  • SingleStringCharacter is defined as:

    SingleStringCharacter ::
           SourceCharacter but not one of ' or \ or LineTerminator
           \ EscapeSequence

    [^'\\\n\r\u2028\u2029] corresponds to the first rule

    \\(?:['"\\bfnrtv]|[^\n\r\u2028\u2029'"\\bfnrtvxu0-9]|0(?![0-9])|x[0-9a-fA-F]{2}|u[0-9a-fA-F]{4}) corresponds to the second rule

    \\(?:\n|\r\n|\r(?!\n)|[\u2028\u2029]) corresponds to the third rule

  • Let's look at the first rule: SourceCharacter but not one of ' or \ or LineTerminator. This first rule deals with "normal" characters.

    SourceCharacter is any Unicode unit.

    LineTerminator is Line Feed <LF> (\u000A or \n), Carriage Return <CR> (\u000D or \r), Line Separator <LS> (\u2028) or Paragraph Separator <PS> (\u2029).

    So we will just use a negative character class to represent this rule: [^'\\\n\r\u2028\u2029].

  • For the second rule, which deals with escape sequences, you can see \ before EscapeSequence, as it appears in the regex. As for EscapeSequence, this is its grammar (strict mode):

    EscapeSequence ::
            0 [lookahead ? DecimalDigit]

    ['"\\bfnrtv]|[^\n\r\u2028\u2029'"\\bfnrtvxu0-9] is the regex for CharacterEscapeSequence. It can actually be simplified to [^\n\r\u2028\u2029xu0-9]

    The first part is SingleEscapeCharacter, which includes ', ", \, and for control characters b, f, n, r, t, v.

    The second part is NonEscapeCharacter, which is SourceCharacter but not one of EscapeCharacter or LineTerminator. EscapeCharacter is defined as SingleEscapeCharacter, DecimalDigit or x (for hex escape sequence) or u (for unicode escape sequence).

    0(?![0-9]) is the regex for the second rule of EscapeSequence. This is for specifying null character \0.

    x[0-9a-fA-F]{2} is the regex for HexEscapeSequence

    u[0-9a-fA-F]{4} is the regex for UnicodeEscapeSequence

  • The third rule deals with string that spans multiple lines. Let's look at the grammar of LineContinuation and other related:

    LineContinuation ::
            \ LineTerminatorSequence
    LineTerminatorSequence :: 
            <CR> [lookahead ? <LF> ]
            <CR> <LF>

    \\(?:\n|\r\n|\r(?!\n)|[\u2028\u2029]) corresponds to the above grammar.


Try this one:


Test it in your console:


It'll match

  • Any number of characters that are:
    • NOT ' or \ (except)
    • \' (or)
    • \ (not followed by ')

If you want it to match the entire string, use the ^ start-of-string and $ end-of-string markers:


... which will match 'string', 'string\'s are awesome' but not 'string's are awesome' or 'string's


it's not that hard...

Also, you need to detect some other possible chars sequences like \n, \r or \\, breaking a line without escaping is not valid in javascript, you must use the \n sequence.


In execution:

var sample = ["'abcdefg'", // Valid
              "'abc\\'defg'", // Valid
              "'abc\\'de\\'fg'", // Valid
              "'abc\\'\\r\\nde\\'fg'", // Valid
              "'abc\\'\r\nde\\'fg'", // Invalid
              "'abc'def'" // Invalid
for(var i = 0; i < sample.length; i++)
    console.log(sample[i].match( /^'([^\\'\n\r]|\\'|\\n|\\r|\\\\)*'$/ ));
  1. ^ tell to the matcher that the next condition must match the begining of the string
  2. ' will match the ' delimiter
  3. ( opens a group
  4. [^\\'\n\r] matches anything different from \ and ', and will not match the special \n and \r characters
  5. | if the condition above didn't match anything, the right side of | will be tested
  6. \\' will match \'
  7. \\n will match a \n literal string
  8. |\\r or will match a \r literal string
  9. |\\\\ or will match a \\ literal string
  10. )* close the group and allow it to repeat multiple times and allow it to do not exist (empty string for example)
  11. ' will match the final ' delimiter
  12. $ tell to the matcher that this must be the and of the string

Try this


See example here

str = 'abc\'de\'fg';

match = str.match(/^([a-z\\']+)$/g);

Tested in Firebug console. Works with or without the escape chars.


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.