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 Pattern | String 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" |