Topic: JavaScript regular expressions
Definition of regular expressions:
Var reg_pattern = new RegRxt("ad"); //It means a is followed by a number
Var reg_pattern = /ad/;
String object related The regular expression method
Name
Description
Example
Macth(regExp)
Specifies the regular expression to search the string, if found the function returns an array, if not found returns Null
test.html
Replacre(regExp,replaceText)
Replace all substrings matching the regular expression regExp in the original string with replaceText and return a new string.
test.html
Search(regExp)
Search string, different from macth(regExp), only returns the position index of the first matching occurrence, otherwise -1 is returned
Character class:
Character list : Specify a character list composed of one or more characters within square brackets. If it matches any character
in the character list, it will be considered a match. For example, [abc] means matching any one of a or b or c. But each match can only match one character in the list, not multiple
Reverse list: Reverse list refers to matching all characters except the specified characters in the list. Reverse lists are defined by preceding the list with the
"^" symbol. For example, [^abc] means matching all characters except a, b, c characters.
Character range: For a continuous character list, such as [1234567] or [abcdefg], it can be expressed in a simple
way, which is called a character range. The character range uses the "-" symbol to connect the starting character and the ending character
, which represents all characters between the starting character and the ending character. For example: [a-z] means all lowercase characters between a~z. Reverse range: Adding the "^" symbol in front of the character means matching any character outside the character range.
With definition character class
Character class
meaning
·
matches any single character except the newline n and carriage return characters, equivalent to [^nr]
d
matches a numeric character , equivalent to [0-9]
D
matches a non-numeric character, equivalent to [^0-9]
w
matches any single character including an underscore, including A~Z,a~z ,0~9 and underscore "_", equivalent to [a-zA-Z0-9_]
W
is complementary to w, matching any non-single character, equivalent to [^a-zA-Z0-9_ ]
s
matches any Unicode whitespace character, including spaces, tabs, formfeeds, etc., equivalent to [ftnr]
S
matches any non-whitespace character
b
backspace character Backspace
Number of repetitions (quantifier)
*: Indicates that the matching of the previous expression occurs zero or more times
For example:
Var reg_pattern = /bo*/;
will match the following strings:
b
bo
boo
+: Indicates that the match of the previous expression appears one or more times in a row.
For example:
Var reg_pattern = /bo+/;
will match the following strings:
bo
booo
{n}: n is a non-negative integer. Indicates that the previous expression must be matched a certain number of n times.
For example:
Var reg_pattern = /bo{3}/;
can only match "booo".
{n,}: n is a non-negative integer. Indicates that the match of the previous expression occurs at least n times, that is, it appears n times
or more than n times.
For example:
Var reg_pattern = /[A-Z]{3,}/g;
Match 3 or more consecutive uppercase letters.
{n,m}: n and m are both non-negative integers. where n < m. Indicates a minimum match of n times and a maximum of m matches.
or more than n times.
Expression:
Var reg_pattern = /[A-Z]{3,5}/g;
matches 3 to 5 consecutive uppercase letters.
For example, the regular expression for searching for mobile phone numbers starting with "133":
Var reg_pattern= /133d{8}/g;
Boundary character:
^: Indicates the starting position of the string, in multi-line matching , indicating the starting position of a line. The symbol itself does not match any characters.
$: indicates the end position of the string. In multi-line matching, it indicates the end position of a line. The symbol itself does not
match any characters
b: represents a word (not character) boundary, that is, the position between a word and a space,