First, some small skills about re

1, the RegExp. Prototype. The exec ()

object Attributes/indexes describe
result [0] All matched strings
[1], ...[*n* ] Group capture in parentheses
index The matched character is at the 0 – based index of the original string 4 `
input Raw string
re lastIndex The position where the next match starts
ignoreCase Is it used?”iThe “tag causes the re match to ignore case true
global Is it used?”g“Flag for global matching. true
multiline Is it used?”m“The tag makes the re work in multi-line mode (that is, ^ and $can match the beginning and end of every line in the string (lines are split by \n or \r), not just the beginning and end of the entire input string.) false
source The string that the re matches
let findDot = /\./g;
findDot.exec('13.156.4 d -);
findDot.exec('13.156.4 d -);
Copy the code

2, String. Prototype. MatchAll ()

The matchAll() method returns an iterator containing the results of all matching regular expressions and the grouped capture groups.

let findDotMatchIterator = '13.156.4 d -.matchAll(/\./g);
findDotMatchIterator.next().value.index;
findDotMatchIterator.next().value.index;
Copy the code

3, the RegExp. Prototype. Multiline

The multiline attribute indicates whether the regular expression uses the “m” flag. Multiline is a read-only property of a regular expression instance.

/^h/g.exec('\nb\nh\nb\nb')
/^h/mg.exec('\nb\nh\nb\nb')
Copy the code

4. The value obtained by the subexpression

'abcs1312das'.replace(/(\d)/g.'[$1]')
/(hello)(word)\2/.exec('hellohellowordwordhelloaacc')
Copy the code

5, groups

const re = / (? 
      
       \d{4})(\.|\/|-)(? 
       
        \d{2})(\.|\/|-)(? 
        
         \d{2})/
        
       
      
const match = re.exec('2021.01-20')

console.log(match.groups)
Copy the code

Zero width assertion

What is a zero-width assertion and what does it do

Zero-width assertions are a way of using regular expressions to find something that comes before or after (but does not include) something. Matching in regular expressions has two concepts, one is matching characters and the other is matching positions

expression In the form of case
(? =pattern) Zero width first assertion /x(? =y)/.exec('xy')
(? ! pattern) Zero width antecedent negation assertion /x(? ! y)/.exec('xc')
(? <=pattern) Zero-width trailing assertion / (? <=x)y/.exec('xy')
(? <! pattern) Zero-width trailing negative assertion / (? <! x)y/.exec('cy')

First assertion'123121231.3'.replace(/(\d)(? =(\d{3})+($|\.) )/g.'$1') / / micrometerPrior negation assertion/Windows (? ! (xp|2000))/ig.exec('Windows 10') after assertion'123. 13132.1365 s.s.. 5156 '.replace(/ (? < = \.. *)[\.].*/g.' ') followed by negative assertion'113dasdasd1231231asdasdadsasda'.replace(/ (? 
      .' ')
Copy the code

Three, amway a component (input box can only input legitimate numbers)

Denver address