Introduction to javascript regular expressions, regular expressions
Overview
A regular expression is a set of rules that are used to match characters in a string.
Basic syntax
Metacharacters
In regular expression mode, some characters have special meanings and are called metacharacters. Metacharacters are matched for a single character.
\ W matches any one of the upper and lower case English characters and numbers 0 to 9 and underline, equivalent to [a-zA-Z0-9 _]
\ W does not match any one of the upper and lower case English characters and numbers 0 to 9, equivalent to [^ a-zA-Z0-9 _]
\ S matches any blank characters, which is equivalent to [\ f \ n \ r \ t \ v]
\ S matches any non-blank characters, which is equivalent to [^ \ s]
\ D matches a single number between 0 and 9, which is equivalent to [0-9]
\ D does not match any single number between 0 and 9, which is equivalent to [^ 0-9]
[\ U4e00-\ u9fa5] matches any single Chinese character (here it uses Unicode encoding to represent Chinese characters)
Identifier
The common Regular Expression Delimiter is a double slash, regex, or/
Atomic Concept
The atoms in a regular expression are divided into visible atoms and invisible atoms.
[\ F \ n \ r \ t \ v] is an invisible atom. Others indicate visible atoms.
Quantifiers
\ * Matches 0 to multiple metacharacters, equivalent to {0 ,}
? Matches 0 to 1 metacharacters, equivalent to {0, 1}
{N} matches n metacharacters
{N,} matches at least n metacharacters
{N, m} matches n to m metacharacters
\ + Matches at least 1 metacharacter, equivalent to {1 ,}
Boundary
\ B match word boundary
^ The string must start with a specified character
$ The string must end with a specified character
Capture Group
In a regular expression, a number of units (which can be characters or regular expressions) are grouped together to form an independent unit.
In a regular expression, a group is divided into a capture group and a non-capture group.
/(pattern)/flags
Pattern correction
Greedy/lazy, case insensitive, ignore blank characters
Use Cases
Form Verification, template engine
The above is all the content of this article. I hope you will like it.