JavaScript Regular Expression Syntax

JavaScript FAQ | JavaScript Strings and RegExp FAQ  

Question: What regular expression syntax/elements can I use in JavaScript?

Answer: You can create a JavaScript regular expression using

  • the RegExp constructor:   re = new RegExp('elements','flags')   or
  • a RegExp literal     re = /elements/flags

    Whether you use constructors or literals, your regular expressions can include the following elements and flags. The elements describe the pattern you want to match, while the flags specify how the matching should be performed.

    Element            Matches this...
    [xyz]              Any one character x or y or z, as listed in square brackets
    [x-z]              Any one character within the range specified in brackets
    [^xyz]             Any one character except x, y, z
    [^x-z]             Any one character except the range in brackets
    .                  Any character except newline
    \d \D              Any digit (\d), any non-digit (\D) 
    \s \S              Any whitespace (\s), any non-whitespace (\S) 
    \w \W              Any word character (\w), any non-word character (\W) 
    \b \B              Word boundary (\b), non-boundary (\B)
    [\b]               Backspace (the square brackets help distinguish backspace from word boundary)
     ^                 Beginning of string; beginning of line if the m flag is set
     $                 End of string; end of line if the m flag is set
    \n \r \f \t \v	   Newline, carriage return, form feed, tab, vertical tab
    \+ \- \. \* \? \|  Match + - . * ? | etc. verbatim (not as a special character)
    \/ \\ \^ \$        Match / \ ^ $ verbatim (not as a special character)
    \[ \] \( \) \{ \}  Match brackets/parentheses/braces verbatim
    \xAB               Match the character with hexadecimal code AB
    \uABCD             Match the Unicode character with hexadecimal code ABCD
    
    x|y                Match x or y
    +                  Match the preceding element one or more times
    *                  Match the preceding element zero or more times
    ?                  Match the preceding element zero or one time
    {n}                Match the preceding element exactly n times
    {n,}               Match the preceding element n or more times
    {m,n}              Match the preceding element at least m, at most n times
    
    (...)              Match ... as a capturing group: store the matching substring
                       (used with string.match(re) to return an array of matching strings)
    \1 \2 \3 etc.	   Match the same substring as in capturing group number 1, 2, 3 etc. 
    $1 $2 $3 etc.	   Replace with the characters that matched group number 1, 2, 3 etc.
                       (used in the second argument of replace() method)
    (?:...)            Match ... as a non-capturing group: grouping only, no storing
    
    Any character      Match the character verbatim
    (except the above)
    

    RegExp flags can be any combination of the following:

        i       Ignore case (both lowercase and uppercase letters will match)
        g       Global (allow multiple matches)
        m       Multiline (^ and $ will match the beginning/end of lines)
    
    The effect of not setting these flags is as follows:
    i not set   The regular expression is case-sensitive
    g not set   Use only the first match (no global search)
    m not set   ^ and $ match the beginning/end of the entire string
    

    Notes
    (1) A whitespace (\s) in a regular expression matches any one of the characters: space, formfeed (\f), newline (\n), return (\r), tab (\t), vertical tab (\v), non-breaking space (\xA0), as well as the Unicode characters \u00A0 \u2028 \u2029.

    (2) When using the RegExp constructor: for each backslash in your regular expression, you have to type \\ in the RegExp constructor. (In JavaScript strings, \\ represents a single backslash!) For example, the following regular expressions match all leading and trailing whitespaces (\s); note that \\s is passed as part of the first argument of the RegExp constructor:

    re = /^\s+|\s+$/g
    re = new RegExp('^\\s+|\\s+$','g') 
    

    Copyright © 1999-2011, JavaScripter.net.