Home > Web Front-end > JS Tutorial > body text

Detailed description of JS regular expressions

零到壹度
Release: 2018-04-12 11:09:13
Original
1205 people have browsed it

Regular expressions are objects that describe character patterns. It is used to match string patterns and search and replace. It is a powerful tool for performing pattern matching on strings.

Syntax
var patt=new RegExp(pattern,modifiers);
var patt=/pattern/modifiers;
pattern (pattern) describes the pattern of the expression
modifiers (modifiers) are used to specify global matching, case-sensitive matching and multi-line matching
Note: When using the constructor to create a regular object, a regular Character escaping rules (preceded by backslash \). For example, the following are equivalent:
var re = new RegExp(“\w ”);
var re = /\w /;

Modifier
Modifiers are used to perform case-sensitive and global matching:

Modifier Description
i Perform case-insensitive matching.
g Perform a global match (find all matches instead of stopping after the first match is found).
m Perform multi-line matching.

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

Expression description
[abc] Find any characters between square brackets.
[^abc] Find any characters not between square brackets.
[0-9] Find any number from 0 to 9.
[a-z] Find any character from lowercase a to lowercase z.
[A-Z] Finds any character from uppercase A to uppercase Z.
[A-z] Finds any character from uppercase A to lowercase z.
[adgk] Find any character within the given set.
[^adgk] Find any character outside the given set.
(red|blue|green) Find any specified option.

Metacharacters
Metacharacters are characters with special meanings:

Metacharacter description
. Finds a single character, except newlines and line terminators.
\w Find word characters.
\W Find non-word characters.
\d Find numbers.
\D Find non-numeric characters.
\s Find whitespace characters.
\S Find non-whitespace characters.
\b Match word boundaries.
\B Matches non-word boundaries.
\0 Find NULL characters.
\n Find newline characters.
\f Find the form feed character.
\r Find the carriage return character.
\t Find tab characters.
\v Find vertical tab character.
\xxx Search for the character specified by the octal number xxx.
\xdd Search for the character specified by the hexadecimal number dd.
\uxxxx Finds the Unicode character specified by the hexadecimal number xxxx.

Quantifier
Quantifier Description
n
Matches any string containing at least one n.

For example, /a / matches the "a" in "candy" and all the "a"s in "caaaaaaandy".

n*
Matches any string containing zero or more n.

For example, /bo*/ matches the "boooo" in "A ghost booooed" and the "b" in "A bird warbled", but does not match "A goat grunted".

n?
Matches any string containing zero or one n.

For example, /e?le?/ matches "el" in "angel" and "le" in "angle".

n{X}
Matches a string containing X sequences of n.

For example, /a{2}/ does not match the "a" in "candy," but matches the two "a"s in "caandy," and matches the first two "caaandy." an "a".

n{X,}
X is a positive integer. Matches the preceding pattern n if it occurs at least X times in a row.

For example, /a{2,}/ does not match the "a" in "candy", but matches all "a"s in "caandy" and "caaaaaaandy."

n{X,Y}
X and Y are positive integers. The preceding pattern n is matched when it appears at least X times and at most Y times.

For example, /a{1,3}/ does not match "cndy", but matches "a" in "candy,", and two "a"s in "caandy," matches "caaaaaaandy" The first three "a"s. Note that when matching "caaaaaaandy", the match is "aaa" even though the original string has more "a"s.

n$ matches any string ending in n.
^n matches any string starting with n.
?=n matches any string immediately followed by the specified string n.
?!n matches any string that is not immediately followed by the specified string n.

RegExp Object Methods
Method Compile regular expressions.                                                                                                                                                                Returns the found value and determines its position. 1 4
test Retrieve the value specified in the string. Returns true or false.                                                                                                   Retrieve values ​​that match a regular expression.​ ​ ​ 1 4
match Find a match for one or more regular expressions. 1 4
replace Replace the substring matching the regular expression.​ ​ 1 4
split Split the string into a string array. 1 4

The above is the detailed content of Detailed description of JS regular expressions. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!