Regular expressions are pattern matching, or matching location, or matching character
1. What position?
Position is a position between adjacent characters, such as hello, is the location where the arrow
2, how to match the position?
In ES5, a total of six characters:
1 ^ beginning matching 2 $ matches the end . 3 \ B word boundary . 4 \ B \ B opposite, non-word boundary . 5 (? = P) a subpattern p, i.e. in front of the p-position . 6 (?! P) (? = p) the opposite
Here we introduced one by one these characters
And $ 2.1 ^
^ (Caret) matches the beginning, the beginning of the match line in multi-line matching.
$
(Dollar sign) the end of the match, matching end of the line in multi-line matching.
For example, we put at the beginning and end of the string with a "#" with (the position can be replaced with characters!):
1 var result = "hello".replace(/^|$/g, '#'); 2 console.log(result); 3 // => "#hello#"
2.2 \ b and \ B
\b
It is a word boundary, that is, specific \w
and \W
location between, but also \w
and ^
position between, but also \w
and $
position between.
For example, a file name is "[JS] Lesson_01.mp4" are \b
as follows:
1 var result = "[JS] Lesson_01.mp4".replace(/\b/g, '#'); 2 console.log(result); 3 // => "[#JS#] #Lesson_01#.#mp4#"
Such as the example above, all \B
replaced by "#":
1 var result = "[JS] Lesson_01.mp4".replace(/\B/g, '#'); 2 console.log(result); 3 // => "#[J#S]# L#e#s#s#o#n#_#0#1.m#p#4"
2.3 (? = P) and (?! P)
(?=p)
Which p
is a sub-mode, i.e., p
the front position.
For example (?=l)
, indicates that the 'l' character positions, for example:
1 var result = "hello".replace(/(?=l)/g, '#'); 2 console.log(result); 3 // => "he#l#lo"
And (?!p)
that is (?=p)
the opposite of meaning, such as:
1 var result = "hello".replace(/(?!l)/g, '#'); 2 3 console.log(result); 4 // => "#h#ell#o#"
3. Case in point:
3.1 Digital micrometer separator notation
Such as the "12345678", becomes "12,345,678."
step1: come up with a final comma
var result = "12345678".replace(/(?=\d{3}$)/g, ',') console.log(result); // => "12345,678"
Come up with all the commas
1 var result = "12345678".replace(/(?=(\d{3})+$)/g, ',') 2 console.log(result); 3 // => "12,345,678"
3.2 verification code issues
6-12 password length, digital, lowercase and uppercase letters, but must include at least two characters.
3.2.1 If you do not consider contain 2 characters can easily write
1 var reg = / ^ [0-9A-Za-z] {6,12} $ /;
3.2.2 to determine whether to include certain characters
Assumptions must contain lowercase letters how to do? Can (? =. * [0-9])
* Matches any single character times
. *? Matches the condition is satisfied only once
var reg = 0909AZaz612;
3.2.3 colleagues include specific two character
var reg = 09az09AZaz612;
3.2.4 contain at least two characters
var reg = 09az09AZ . * [az]) (? =. * [AZ])) ^ [0-9A-Za-z] {6,12} $ /;
3.2.5 Another solution: contains at least two characters that can not be all digital, can not be all lowercase letters, not all uppercase letters
var reg = / (?! ^ [0-9] {6,12} $) (?! ^ [z] {6,12} $) (?! ^ [AZ] {} $ 6.12) ^ [ 0-9a-Za-z] {} $ 6.12 /;