English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

JavaScript RegExp (RegExp) Object

Regular expressions are a series of characters that form a search pattern.

When searching for data in the text, you can use this search pattern to describe the content to be searched.

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

RegExp object

A regular expression is an object that describes a character pattern.

RegExp The constructor creates a regular expression object that matches text with the pattern.

Syntax:
new RegExp(pattern, modifiers);

or simply

/pattern/modifiers;
var regex = /w3codebox/g

Example explanation:

  • w3codeboxis a pattern (used for search)

  • gis a modifier (perform global matching)

You can find in ourIn the JavaScript RegExp tutorialLearn more about regular expressions.

Modifier

Modifiers are used to perform global search, case-sensitive, and multi-line search:

ModifierDescription
gExecute global matching, that is, find all matches instead of stopping after the first match
iExecute case-insensitive matching
mExecute multi-line matching

Parentheses

Brackets are used to find a series of characters:

ExpressionDescription
[abc]Matches any character in brackets
[^abc]Matches any character not in brackets
[0-9]Matches any number between brackets
[^0-9]Matches any character not between parentheses (any non-digit character)
(x|y)Matches any specified alternative

Meta-character

A meta-character is a letter character followed by a backslash to make the combination have a special meaning:

Meta-characterDescription
.Matches any single character except the newline character \n.
\wMatches letters and digits [0-9a-zA-Z]
\WMatches any non-letter and non-digit
\dMatches any digit[0-9]
\DMatches any non-digit[^0-9]
\sMatches space, newline, and indentation characters
\SMatches non-space, newline, and indentation characters
\bAt the beginning of the word/End found the match item
\BMatches not at the beginning of a word/End of the match item
\0Matches a single NUL character
\nMatches the newline
\fMatches the form feed
\rMatches the carriage return
\tMatches the tab
\vMatches the vertical tab
\xxxMatches the character specified by the octal number xxx
\xddMatches the character specified by the hexadecimal number dd
\uxxxxMatches the Unicode character specified by the hexadecimal number xxx

Quantifier

The frequency or position of a sequence of bracket characters and a single character can be represented by special characters:

QuantifierDescription
z+Matches any string containing at least onezString
z*Matches any string containing zero or morezString
z?Matches any string containing zero or onezString
z{X}Matches a string containing a sequence of X 'z' characters
z{X,Y}Matches a string containing a sequence of X to Y 'z' characters
z{X,}A string that contains at least X z sequences.
z$Matches any string that starts withzA string that ends with
^zMatches any string that starts withzA string that starts with
?=zMatches a string followed by a specific stringzany string
?!zMatches any string not followed by a specific stringzof the string

RegExp Object Properties

The following table lists the properties of the RegExp object:

PropertiesDescription
constructorReturns the function that creates the prototype of the RegExp object
globalChecks if the 'g' modifier is set
ignoreCaseChecks if the 'i' modifier is set
lastIndexStarts the index of the next match
multilineChecks if the 'm' modifier is set
sourceReturns the text of the RegExp pattern

RegExp Object Methods

The following table lists the methods of the RegExp object:

MethodDescription
exec()Searches for a match in the string parameter
test()Tests whether the string parameter matches and returns a boolean value
toString()Returns a string representation of the specified object