Home > Article > Web Front-end > Deeply understand the analysis of greedy mode and non-greedy mode of JS regular expressions
This article mainly introduces the analysis of greedy mode and non-greedy mode for in-depth understanding of JS regular expressions. It has certain reference value. Now I share it with you. Friends in need can refer to it
mentioned regular quantifiers before, but the quantifiers will bring about the problem of which should be matched.
\d{3,6}This regular expression matches 3 to 6 numbers, but when this regular expression is used to match the
12345678 string , should it match three numbers, six numbers, or neither?
let text = '12345678' let reg = /\d{3,6}/g text.replace(reg, 'X') // X78You can see that this regular expression replaces the six numbers
123456 with
X, that is, in normal mode , the regular expression will match as many matches as possible.
? after the quantifier.
let text = '12345678' let reg = /\d{3,6}?/g text.replace(reg, 'X') // X45678It can be found that in non-greedy mode, this regular pattern only matches
123, which is the minimum match.
In-depth understanding of the analysis of quantifiers of JS regular expressions
In-depth understanding of the analysis of JS regular expressions Analysis of predefined classes and boundaries
The above is the detailed content of Deeply understand the analysis of greedy mode and non-greedy mode of JS regular expressions. For more information, please follow other related articles on the PHP Chinese website!