Common metacharacters

\ s \ s not blank \ w Numbers, letters, underline the \ \ w + w \ \ p p letter not letter means to match one or more, greedy pattern matching multiple or zero {n} * said limit n, or {m, n} m to n? Zero or one/I match case insensitive /g match all /u matches using UTF-8. Fix for wide-byte /m multi-line matching to stop /y matching when it is not, which can increase efficiency in certain circumstances, such as knowing that the targets are together and /gi can match both of themCopy the code

The test case

User name verification

Let the userName = 'mkmin console. The log (/ ^ [a-z] {3, 6} /. The test (userName))Copy the code

The phone number

Let tel = '020-12345678 console. The log (/ (010 | 020) \ \ d {7, 8} / test (tel) console. The log (/ [19] \ - \ {7, 8} d/test (tel))Copy the code
let tel='020-12345678'
console.log(/[^\d-]/.test(tel))
Copy the code

Whether multiple lines conform to the format

let str=`
a=1,
b=2
c=3
`
console.log(str.match(/\w=\d,/gm))
Copy the code

Check with attribute match \p{L}

Log (userName. Match (/\P{N}/gu))// Check whether there are non-numbers console.log(userName. Match (/\P{N}/gu))// Check whether there are numbers Console. log(userName. Match (/\P{L}/gu))// Checks for non-lettersCopy the code

LastIndex needs to be used globally

const test='cesh' const reg1=/\w/g while(res1=reg1.exec(test)){ console.log(reg1.lastIndex)} const reg2=/\w/ While (res2=reg2.exec(test)){console.log(reg2.lastindex)} It's always going to be 0Copy the code

Atomic group verification date

let time1='2020-01-01'
let time2='2020/01/01'
let time3='2020-01/01'
console.log(/^\d{4}([-\/])\d{2}\1\d{2}/.test(time1))
console.log(/^\d{4}([-\/])\d{2}\1\d{2}/.test(time2))
console.log(/^\d{4}([-\/])\d{2}\1\d{2}/.test(time3))
// Use \1 to get the first atomic group
Copy the code

Elimination of atomic groups

let time1='2020-01-01'
console.log(/[^\d]+/g.test(time1))
console.log(/[^\d\-]+/g.test(time1))
Copy the code

Atomic group verification mailbox

const email1='[email protected]' const email2='[email protected]' const reg=/\w+@(\w+.) +/ console.log(reg.test(email1)) console.log(reg.test(email2))Copy the code

Atomic group and replace’s $

Var STR = "< 1, 2 >"; str.replace(/(\d),(\d)/, "===$1===$2===$`===%'===$&");Copy the code

Use? Ban greed

let str='<span>1</span><span>2</span>' const str1=str.replace(/<span>([\s\S]+?) <\/span>/gi,'$1,mkmin') console.log(str1) const str2=str.replace(/<span>([\s\S]+)<\/span>/gi,'$1,mkmin') console.log(str2)Copy the code

The realization of the matchAll

String.prototype.matchAll=function(reg){ let res=this.match(reg) if(res){ let str=this.replace(res[0],'^'.repeat(res[0].length)) let match=str.matchAll(reg)||[] return [res,...match] } } let str='<h1>1</h1><h1>2</h1>' console.log(str.matchAll(/<h1>([\s\S]+?) <\/h1>/))Copy the code

Use the search method to get the subscripts that appear

The search method can search only onceCopy the code
const str='mkmin'
console.log(str.search(/M/gi))
Copy the code