HTML CSS Bootstrap Javascript Icons Python
JavaScript Reference
Overview
JS Reference
JS Array Object JS Boolean Object JS Date Object JS Style Declaration Object JS Error Object JS Global Object JS Math Object JS Number Object JS String Object JS RegExp Object JS Statements
DOM Reference
DOM Attribute DOM Console DOM Document DOM Element DOM Events DOM Event Objects DOM History DOM HTMLCollection DOM Location DOM Navigator DOM Screen DOM Style DOM Window WEB Storage

JavaScript Regular Expression Reference

A regular expression is a sequence of characters that forms a search pattern.

When you search for data in a text, you can use this search pattern to describe what you are searching for.

A regular expression can be a single character, or a more complicated pattern.

RegExp Object

A regular expression is an object that describes a pattern of characters.

The RegExp constructor creates a regular expression object for matching text with a pattern.

Syntax:
new RegExp(pattern, modifiers);

or simply

/pattern/modifiers;

Example explained:

You can learn more about Regular Expressions in our JavaScript RegExp Tutorial.


Modifiers

Modifiers are used to perform global searches, case sensitivity and searching in multiple lines:

Modifier Description
g Performs a global match that is, find all matches rather than stopping after the first match
i Perform case-insensitive matching
m Perform multiline matching

Brackets

Brackets are used to find a range of characters:

Expression Description
[abc] Find any character between the brackets
[^abc] Find any character NOT between the brackets
[0-9] Find any character between the brackets (any digit)
[^0-9] Find any character NOT between the brackets (any non-digit)
(x|y) Find any of the alternatives specified

Metacharacters

A metacharacter is simply an alphabetical character preceded by a backslash that acts to give the combination a special meaning:

Metacharacter Description
. Find a single character, except newline or line terminator
\w Find a word character
\W Find a non-word character
\d Find a digit
\D Find a non-digit character
\s Find a whitespace character
\S Find a non-whitespace character
\b Find a match at the beginning/end of a word
\B Find a match not at the beginning/end of a word
\0 Find a NUL character
\n Find a new line character
\f Find a form feed character
\r Find a carriage return character
\t Find a tab character
\v Find a vertical tab character
\xxx Find the character specified by an octal number xxx
\xdd Find the character specified by a hexadecimal number dd
\uxxxx Find the Unicode character specified by a hexadecimal number xxxx

Quantifiers

The frequency or position of bracketed character sequences and single characters can be denoted by a special character:

Quantifier Description
z+ Matches any string that contains at least one z
z* Matches any string that contains zero or more occurrences of z
z? Matches any string that contains zero or one occurrences of z
z{X} Matches any string that contains a sequence of X z's
z{X,Y} Matches any string that contains a sequence of X to Y z's
z{X,} Matches any string that contains a sequence of at least X z's
z$ Matches any string with z at the end of it
^z Matches any string with z at the beginning of it
?=z Matches any string that is followed by a specific string z
?!z Matches any string that is not followed by a specific string z

Regular Expression Object Properties

The following table lists the properties of the RegExp object:

Property Description
constructor Returns the function that created the prototype of RegExp object
global Checks whether the "g" modifier is set
ignoreCase Checks whether the "i" modifier is set
lastIndex The index at which to start the next match
multiline Checks whether the "m" modifier is set
source Returns the text of the RegExp pattern

Regular Expression Object Methods

The following table lists the methods of the RegExp object:

Method Description
exec() Executes a search for a match in its string parameter
test() Tests for a match in its string parameter and return a boolean value
toString() Returns a string representing the specified object