English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
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.
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.
Modifiers are used to perform global search, case-sensitive, and multi-line search:
Modifier | Description |
---|---|
g | Execute global matching, that is, find all matches instead of stopping after the first match |
i | Execute case-insensitive matching |
m | Execute multi-line matching |
Brackets are used to find a series of characters:
Expression | Description |
---|---|
[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 |
A meta-character is a letter character followed by a backslash to make the combination have a special meaning:
Meta-character | Description |
---|---|
. | Matches any single character except the newline character \n. |
\w | Matches letters and digits [0-9a-zA-Z] |
\W | Matches any non-letter and non-digit |
\d | Matches any digit[0-9] |
\D | Matches any non-digit[^0-9] |
\s | Matches space, newline, and indentation characters |
\S | Matches non-space, newline, and indentation characters |
\b | At the beginning of the word/End found the match item |
\B | Matches not at the beginning of a word/End of the match item |
\0 | Matches a single NUL character |
\n | Matches the newline |
\f | Matches the form feed |
\r | Matches the carriage return |
\t | Matches the tab |
\v | Matches the vertical tab |
\xxx | Matches the character specified by the octal number xxx |
\xdd | Matches the character specified by the hexadecimal number dd |
\uxxxx | Matches the Unicode character specified by the hexadecimal number xxx |
The frequency or position of a sequence of bracket characters and a single character can be represented by special characters:
Quantifier | Description |
---|---|
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 |
^z | Matches any string that starts withzA string that starts with |
?=z | Matches a string followed by a specific stringzany string |
?!z | Matches any string not followed by a specific stringzof the string |
The following table lists the properties of the RegExp object:
Properties | Description |
---|---|
constructor | Returns the function that creates the prototype of the RegExp object |
global | Checks if the 'g' modifier is set |
ignoreCase | Checks if the 'i' modifier is set |
lastIndex | Starts the index of the next match |
multiline | Checks if the 'm' modifier is set |
source | Returns the text of the RegExp pattern |
The following table lists the methods of the RegExp object:
Method | Description |
---|---|
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 |