JavaScript RegExp Reference Manual

RegExp Object

Regular Expression is a character pattern.

This pattern is used for the 'search and replace' feature that matches patterns in text.

In JavaScript, the RegExp object is a pattern that has properties and methods.

Syntax

/pattern/modifier(s);

Example

let pattern = /codew3c/i;

Try it yourself

Example explanation:

codew3c Pattern to search
/codew3c/ Regular Expression
/codew3c/i Case-insensitive regular expression

For knowledge about regular expressions, please read our JavaScript RegExp Tutorial.

Modifiers

Modifiers are used to perform case-insensitive and global searches:

Modifiers Description
g Perform global matching (find all matches instead of stopping after the first match).
i Perform case-insensitive matching.
d Perform substring matching.
m Perform multi-line matching.

Square brackets

Square brackets are used to find characters within a certain range:

Expression Description
[abc] Find any character within parentheses.
[^abc] Find any character not within square brackets.
[0-9] Find any digit from 0 to 9.
[^0-9] Find any character not within parentheses (any non-numeric).
(x|y) Find any specified option.

Meta Characters

Meta characters are characters that have special meaning:

Meta Characters Description
. Find a single character, except a newline or end-of-line.
\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, where the start is as follows: \bHI, and the end is as follows: HI\b.
\B Find a match but not at the beginning/end of a word.
\0 Find a NULL character.
\n Find a newline.
\f Find a form feed.
\r Find a carriage return.
\t Find a tab.
\v Find a vertical tab.
\xxx Find a character specified by an octal number xxx.
\xdd Find a character specified by a hexadecimal number dd.
\udddd Find a Unicode character specified by a hexadecimal number xxxx.

Quantifiers

Quantifiers Description
n+ Match any string that contains at least one occurrence of n.
n* Match any string that contains zero or more occurrences of n.
n? Match any string that contains zero or one occurrence of n.
n{X} Match any string that contains X occurrences of n.
n{X,Y} Match any string that contains X to Y occurrences of n.
n{X,} Match any string that contains at least X occurrences of n.
n$ Match any string that ends with n.
^n Match any string that starts with n.
?=n Match any string that immediately follows the specified string n.
?!n Match any string that does not immediately follow the specified string n.

RegExp object properties

Properties Description
constructor Returns the function that creates the prototype of the RegExp object.
global Check if the 'g' modifier is set.
ignoreCase Check if the 'i' modifier is set.
lastIndex Specifies the index of the start of the next match.
multiline Check if the 'm' modifier is set.
source Returns the text of the RegExp pattern.

RegExp object methods

Method Description
compile() Depreciated in version 1.5. Compiles a regular expression.
exec() Test for a match in a string. Returns the first match.
test() Test for a match in a string. Returns true or false.
toString() Returns the string value of the regular expression.

Description of RegExp object

The RegExp object represents a regular expression, which is a powerful tool for performing pattern matching on strings.

Direct quantity syntax

/pattern/attributes

Syntax for creating RegExp objects:

new RegExp(pattern, attributes);

parameter

parameter pattern is a string that specifies the pattern of the regular expression or another regular expression.

parameter attributes is an optional string that contains the properties "g", "i", and "m", which are used to specify global matching, case-insensitive matching, and multi-line matching, respectively. Before the standardization of ECMAScript, the m property was not supported. If pattern is a regular expression rather than a string, then the parameter must be omitted.

Return value

A new RegExp object with the specified pattern and flags. If the parameter pattern is a regular expression rather than a string, then the RegExp() constructor will create a new RegExp object with the same pattern and flags as the specified RegExp.

If the RegExp() is not used as a function call, but as a function call, then its behavior is the same as when the new operator is called, but when pattern It is a regular expression, it only returns pattern, and no new RegExp object is created.

throws

SyntaxError - If pattern is not a valid regular expression, or attributes contains characters other than "g", "i", and "m", throw this exception.

TypeError - If pattern It is a RegExp object but does not omit attributes If the parameter throws this exception.

Browser support

/regexp/ is a feature of ECMAScript1 (ES1).

All browsers fully support ES1 (JavaScript 1997):

Chrome IE Edge Firefox Safari Opera
Chrome IE Edge Firefox Safari Opera
Supports Supports Supports Supports Supports Supports

Methods of String objects that support regular expressions

Method Description
search Retrieve the value that matches the regular expression.
match Find one or more matches of a regular expression.
replace Replace the substring that matches the regular expression.
split Split a string into an array of strings.