Github is a daily summary of what you and your friends do together

preface

A few weeks did not update, the early review of the re, re in THE USE of JS, said that since learning front-end heard many such “joke”, programmers are divided into regular and not regular.

A regular basis

Special characters + one or more * 0 times or more {n} n times {n,n} how many times to how many times. Other than carriage return? Conversion of greed, match the previous one or zero position / ^ ^ began | or not () groupCopy the code
Common (usually lowercase matches uppercase not) \d matches digits [0-9] \W non-digits \ R carriage return character \ N Newline character \s matches whitespace such as \r\n\f \s not These whitespace character \W numeric character underscores are equivalent to [A-ZA-z_] \W non-numeric character underscores [^ a-za-z_] \b word boundary \b non-word boundary...Copy the code
Other foresight, etc. :) won't improve performance in our regular memory. =B) forward-looking = equivalent to A before the argument is equal to B will be matched by A(? ! B) prospective non-equivalent to the preceding parameter of A equal to B will be matched (? <=B)A is equal to A is equal to B is matched (? <! B) if A is not equal to A, B will not be matchedCopy the code

Use of re (js)

String methods

Where our JS uses regular matching replace does the substitution split what does it split array match searches for the current value and returns the current searched value search is the equivalent of indexOfCopy the code
Search uses 'ab12'. Search (/\d/) // 2' ab12'. Search (/3/) //-1 returns the same value as indexofCopy the code
The split using 'a1a1a1a1a1'. The split (\ d /) / / [" a ", "a", "a", "a", "a", ""]Copy the code
Replace uses' a1a1a1a1A1 '. Replace (/(a)/g, '$1$1') // aa1AA1AA1AA1aa1 $1 is the first group to match. The second argument can also be a function. The second is $1 - $99 for the currently matched groupCopy the code
Match searches and returns // no g returns the same value as regexobj.exec () 'a1a1a1a1A1 '. Match (/1/) returns an array containing the values currently matched by [1] and index of the current match, Input The current matching value, groups an array of capture groups or undefined (if no named capture group is defined). 'a1a1a1a1a1'. Match (/1/g) // ["1","1","1","1","1","1","1"] returns all current matching values if the current regex is /1/gCopy the code

The RegExp method translates’/’ to ‘//’ in the RegExp constructor

Boolean const reg = new RegExp('test') reg.test(/test/) // trueCopy the code
Exec retrieves the value specified in the string. Returns the value found and determines its position. Var reg = new RegExp('test','g') var val = reg.exec('test') var val = reg.exec('test' {0: "test", groups: undefined, index: 0, input: "test", length: 1,} // The main thing is that $1 of the current RegExp will be saved. : test) ', 'g') plus? // reg.lastIndex Note that this property increments to the index of the current comparison in the case of g, which will be used to start the comparison the next timeCopy the code
toString
	var reg = new RegExp('\\s(12)\\1test','g')
    reg.toString() // "/\s(12)\1test/g"
Copy the code

Forward-looking use? The RegExp attribute is used

Let STR = "aaabbbcccbbb" match(/b+(? =c{3})/) let STR = "aaabbbcccbbb" get BBB str.match(/(? <=a+)b+/) ? The use of? Let STR = "aaabcccbbb" str.search(/a+b? <img style="width: 1em; 12px" src="" />' html.replace(/(\<img.*style\=\").*(\".*)/g, "$1 hright: 12 px $2") / / "< img style =" hright: 12 px "/ >" into the lead to SRC = "" out of sight This time we put the HTML. Replace (/ \ < img. * style = \ \ "). *? (\".*)/g, "$1hright:12px$2") add a? <img style="hright:12px" SRC ="" /> 'abababababababab' let reg = /a/g reg.exec(str) console.log(reg.lastIndex) // 1 reg.exec(str) console.log(reg.lastIndex) // 3 reg.exec(STR) console.log(reg.lastindex) // 5  let reg = /a/g reg.exec(str) console.log(reg.lastIndex) // 1 reg.lastIndex = 0 reg.exec(str) console.log(reg.lastIndex) // 1 reg.lastIndex = 0 reg.exec(STR) console.log(reg.lastIndex) // 1Copy the code

Common regular

/ ^ ([0-9] (13) | | (14 [5] | 7) (15 ([0, 3] | [5-9])) | (18 [0, 5-9])) \ d {8} $/ / / phone number / ^ 1-9] [\ d {5} (18 19 | | ([23] \ d)) \ d {2} ((0 [1-9]) | | | 11 (10)) (([0-2] [1-9]) 10 20 | | | | 30 31) \ d {3} [xx] 0-9 $/ / / id / ^ 1-9] [\ d {5} (? ! \d)$/ // zip... Too much do not write baidu on the line =. =Copy the code