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}
12345678 string , should it match three numbers, six numbers, or neither?
let text = '12345678' let reg = /\d{3,6}/g text.replace(reg, 'X') // X78
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') // X45678
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!