RegExp constructor

Regular expressions can be created by using the RegExp constructor.

RegExp constructor accepts two arguments:

  • a pattern
  • an optional string of flags.

The following code:

var pattern1 = /[oo]at/i;

is the same as:

var pattern1 = new RegExp("[oo]at", "i");

All metacharacters must be double-escaped:

Literal PatternString Equivalent
/\[oo\]dr/"\\[oo\\]dr"
/\.dr/"\\.dr"
/name\/age/"name\\/age"
/\d.\d{1,2}/"\\d.\\d{1,2}"
/\w\\hello\\123/"\\w\\\\hello\\\\123"
Home 
  JavaScript Book 
    Essential Types  

REGEXP:
  1. The Regexp Type
  2. RegExp constructor
  3. RegExp Instance Properties
  4. RegExp's exec() does the matching.
  5. RegExp's test()
  6. RegExp's toLocaleString() and toString()
  7. RegExp Constructor Properties