Javascript RegExp Object

In Javascript RegExp constructor is used to match character with a pattern by creating a regular expression object. The regular expression's elements specify what you would like to match or replace. These patterns are used with the 'exec' and 'test' methods of RegExp, and with the match, replace, search, and split methods of String.

Here is the simple syntax to call properties and functions of Array Object.

Syntax

pattern : The text of the regular expression.

flags : Flags is optional but if you specified, flags can have any combination of the following values:

Regular Expression Flags

Flags Description
i This is used for ignore case
g This is used for global match
m This is used for multiline
y This is used to sticky; matches only

There are two ways to create a RegExp object: a literal notation and a constructor.

Javascript RegExp Object Example

Regular Expression Patterns

Expression Description
[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 more times
{n} Match the preceding element exactly n times
{n,} Match the preceding element exactly n times
{m,n} Match the preceding element at least m, at most n times
(...) Match ... as a capturing group: store the matching substring
\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.
(?:...) Match ... as a non-capturing group: grouping only, no storing