String uses methods that can be used with the re
- Match Matches, which can be based on the regex
- Search does not match the position to return -1
- Split can be split as a regular
- Replace can be replaced with the regular str.replace(reg,’b’).
Second, re creation method
- The first creation method
Const reg = /ab/g const STR = 'abcabbaab' reg.test(STR) // string match method, Const strArr = str.match(reg) // ['ab', 'ab', 'ab']Copy the code
- Second creation method
const reg1 = new RegExp('abc', 'i')
console.log(reg1.test('abcd')) // ture
Copy the code
Common matching rules
- Modifier: ** I ignores case; G global matching; M performs multi-line matching **
- /^ in front to indicate start, [^] ^ in front to indicate not
Const reg2 = /^a/gm const str2 = 'abcde\na' console.log(str2.match(reg2)) //['a', 'a'] / / a [] represents a const reg3 = / [^ a]] [^ b/g const str3 = 'ab1cd'. The console log (str3. Match (reg3)) / / / 'b1', 'CD'Copy the code
- \t TAB; \n Newline space; \r Carriage return line end; \v vertical TAB; \ t indentation;
- \ w = = = [0-9 a – z_]; \W ===[^\w]
- = = = d \ [0-9]; \D ===[^\d]
- \s === [\t\n\r\v\f]; \S ===[^\ S] Whitespace character \S contains Spaces
- \b === single boundary; \B === nonsingular boundary
- . === [^\r\n] matches one digit
- \b === word boundary; \B === non-word boundaries
Const reg5 = / bcde\b/g const str5 = 'ABC cde FGH' console.log(str5.match(reg5)) //['cde'] // Match \t const reg6 = /\tc/g const str6 = 'abc\tcdefgh' console.log(str6.match(reg6)) //[ '\tc' ]Copy the code
- Number n* === {0,}
- n+=== {1,}
- n? = = = {0, 1}
- {X} === X;
- N {X,Y} {X,Y} X-Y, greedy matching principle, can be first Y, then X
/ / n * {0} as a match to the empty string const reg7 = / \ d * / g const str7 = 'ABC' in the console. The log (str7. Match (reg7)) / / / ' ' ' ' ' ', '' ] const reg8 = /\w*/g const str8 = 'abc' console.log(str8.match(reg8)) // [ 'abc', ] // n+ {1,} will not match empty strings const reg9 = /\d+/g const str9 = 'ABC' console.log(str9.match(reg9)) // null // n? {0,1} const reg10 = /\w? /g const str10 = 'aaaa' console.log(str10.match(reg10)) // [ 'a', 'a', 'a', 'a', "] // n{X} {X} X const reg11 = /\w{3}/g const str11 = 'aaaaaa' console.log(str11.match(reg11)) // ['aaa', 'aaa'] // n{X,Y} {X,Y} {X,Y} Const reg12 = /\w{2,4}/g const str12 = 'aaaaaa' console.log(str12.match(reg12)) // ['aaaa', 'aa'] / / a string Whether the fore and aft contains digital const reg13 = $/ ^ \ | d \ d/g / / test whether a string fore and aft all contain digital const reg14 = / ^ \ d \ d [\ s \ s] * $/ gCopy the code
- The exec regular G modifier lastIndex returns the one inside the capture ()
- In non-global match mode, this function is the same as the match() function
const reg15 = /ab/g const str15 = 'abab' console.log(reg15.lastIndex) // 0 console.log(reg15.exec(str15)) // [ 'ab', index: 0, input: 'abab', groups: undefined ] console.log(reg15.lastIndex) // 2 console.log(reg15.exec(str15)) // [ 'ab', index: 2, input: 'abab', groups: Undefined] const str5 = 'IT interview questions blog contains many < a href = "http://hi.baidu.com/mianshiti/blog/category/ Microsoft interview questions" > < / a >' Microsoft interview questions const exg5 = /<a.*href="(.*)".*>/g console.log(exg5.exec(str5)) // [ // '<a Href = "http://hi.baidu.com/mianshiti/blog/category/ Microsoft interview questions" > < / a > 'Microsoft interview questions, / / http://hi.baidu.com/mianshiti/blog/category/ Microsoft interview questions, / / index: 12, / / input: 'IT interview blog contains many < a href = "http://hi.baidu.com/mianshiti/blog/category/ Microsoft interview questions" > < / a >' Microsoft interview questions, / / groups: undefined / /]Copy the code
- () or, capture, (? 🙂 do not capture; match replace
/ / () or the relationship between the const reg4 = / ABC | (BCD) [0-9] / g const str4 = 'bcd1'. The console log (str4. Match (reg4)) / / [' bcd1 '] / / () back reference const reg16 = /(\w)\1\1\1/g const str16 = 'aaaabbbb' console.log(str16.match(reg16)) //[ 'aaaa', 'BBBB'] // \w matches alphanumeric _, // \1 matches the first expression (\w), \2 match the second expression (\w) g is a global match const reg17 = /(\w)\1\1(\w)\2\2/g const str17 = 'aaabbbaaabbb' console.log(str17.match(reg17)) // [ 'aaabbb', 'aaabbb' ] console.log(reg17.exec(str17)) //['aaabbb','a','b',index: 0,input: 'aaabbbaaabbb',groups: undefined] console.log(reg17.exec(str17)) //['aaabbb','a','b',index: 6,input: 'aaabbbaaabbb',groups: undefined] console.log(reg17.exec(str17)) //nullCopy the code
- Two classic examples:
Const reg18 = /(\w)\1(\w)\2/g const str18 = 'aabb' console.log(str18.replace(reg18, '$2$2$1$1')) // bbaa console.log( 'str18,fun: ', str18.replace(reg18, function ($, $1, $2) { return $2 + $2 + $1 + $1 }) ) // the-first-name ===> theFirstName const reg19 = /-(\w)/g const str19 = 'the-first-name' console.log( 'str19: ', str19.replace(reg19, function ($, $1) { return $1.toUpperCase() }) )Copy the code
- (? =b) forward pre-check; (? ! B) Forward assertion; (? 🙂 don’t capture
// n? {0,1} const reg10 = /\w? // const reg20 = /a(? /g // a const reg20 = /a(? ! B)/g // a no b ['a', 'a', 'a'] const str20 = 'abaaaab' console.log(str20.match(reg20))Copy the code
- Greedy match followed by? Becomes a non-greedy match
// const reg21 = /a+/g // [ 'aaa' ] // const reg21 = /a+? /g // [ 'a', 'a', 'a' ] const reg21 = /a?? /g // [ '', '', '', '' ] const str21 = 'aaa' console.log(str21.match(reg21)) const reg3 = /<.+? >/g const str3 = 'ada<option value="hh">0</option>54<div id="as">adda</div>ad' console.log(str3.match(reg3)) // [ '<option value="hh">', '</option>', '<div id="as">', '</div>' ]Copy the code
- Classic examples:
// multiple of '10000000000' 3 (\d{3})+ const cestr1 = '10000000' const cereg1 = /(? = (\) B (\ d {3}) + $)/g. The console log (' cestr1: 'cestr1. Replace (cereg1,', ')) / / 1000000 const cereg2 = / (? =(\B)(\d{3})+(? ! \ d))/g the console log (' cestr1 - : 'cestr1. Replace (cereg2,', ')) / / 1000000Copy the code