Home >Web Front-end >JS Tutorial >JS regular expression character matching
Regular expression character matching
These are some notes compiled after reading "JavaScript Regular Expressions Mini Book".
Regular expression is a matching pattern, which can match characters and positions.
The following mainly introduces the situation of matching characters. I am also learning the situation of matching positions.
Two types of fuzzy matching:
1. Horizontal fuzzy matching: The length of a regular matchable string is not fixed. The way this is done is by using quantifiers. For example, {m,n} means that the character appears continuously at least m times and at most n times.
For example, /ab{2,5}c/ means matching a string like this: the first character is "a", followed by 2 to 5 characters "b", and finally the character "c" .
For example: (You can try it manually and think about the results you will get)
var regex = /ab{2,5}c/g; var string = "abc abbc abbbc abbbbc abbbbbc abbbbbbc"; console.log( string.match(regex) );
g is a modifier, which means global matching, that is, finding matching conditions in string in order all strings.
2. Vertical fuzzy matching: A regular matchable string is specific to a certain character. It does not need to be a certain character, and it can have many possibilities. The way it is implemented is to use the character group
such as /a[123]b/ to match such a string: the first character is a, and the second character can be '1', '2', ' Any one of 3', but only one.
Quantifier (repetition)
1. Common abbreviation form:
(1) {m,} means at least m times
(2) {m} means
appears m times (3)? Equivalent to {0,1} means
appears or does not appear (4) Equivalent to {1,} means at least 1 appears Times
(5)* is equivalent to {0,} which means it can appear any number of times, it can not appear or it can be several times
2. Greedy matching and lazy matching
(1) Greedy matching: /\d{2,5}/ means that the number appears 2-5 times in a row and will match as many as possible
var regex = /\d{2,5}/g; var string = "123 1234 12345 123456"; console.log( string.match(regex) ); // => ["123", "1234", "12345", "12345"]
(2) Lazy matching: /\d{2,5} ?/ means that although 2-5 times are enough, when 2 is enough, no more attempts will be made.
var regex = /\d{2,5}?/g; var string = "123 1234 12345 123456"; console.log( string.match(regex) ); // => ["12", "12", "34", "12", "34", "12", "34", "56"]
Character group
1. Range representation:
(1) Use the hyphen "-" to omit the abbreviation, such as [ 123456abcdefGHIJKLM] can be written as [1-6a-fG-M].
(2) Note: If there is a hyphen in the matched string, either put it at the beginning or end, or escape -.
2. Exclude character groups: For example, [^abc]
means that a certain character can be anything, but it cannot be a, b, or c. The ^ caret represents negation, and there is also a corresponding range representation.
3. Common abbreviation form:
(1) \d
means [0-9]
. It is a single digit
(2) \D
means [\^0-9]
. Any character except numbers.
(3) \w
means [0-9a-zA-Z_]
. Numbers, uppercase and lowercase letters, and underscores. Also called the word character
(4) \W
means [^0-9a-zA-Z_]
. Non-word characters
(5) \s
represents [ \t\v\n\r\f]
. Represents whitespace characters, including spaces, horizontal tabs, vertical tabs, line feeds, carriage returns, and form feeds.
(6) \S
means [^ \t\v\n\r\f]
. Non-whitespace characters
(7) .
means [^\n\r\u2028\u2029]
. Wildcard character, representing almost any character. Exceptions include line feeds, carriage returns, line separators, and paragraph separators.
To match any character, use [\d\D]
, [\w\W]
, and [ Any one of ^]
.
Multi-selection branch
A mode can achieve horizontal and vertical fuzzy matching, and the multi-selection branch can support any one of multiple sub-modes.
Specific form: (p1|p2|p3
) p1, p2, p3 are sub-patterns.
Please pay attention to the following issue.
var regex = /good|goodbye/g; var string = "goodbye"; console.log( string.match(regex) );
The result obtained in the above example is "good"
var regex = /goodbye|good/g; var string = "goodbye"; console.log( string.match(regex) );
The result obtained in this example is "goodbye"
We get the conclusion: the branch structure is also Lazy, that is, when the previous match is matched, the subsequent ones will not be tried again.
Recommended tutorial: "JS Tutorial"
The above is the detailed content of JS regular expression character matching. For more information, please follow other related articles on the PHP Chinese website!