Regular expression: (? = A) What does it mean?

 

1. (? = A) means that we need to match in front of something.

2. (?! a) does not mean that we need to match something.

3. (?: a) means that we need to match something in itself.

4. (? <= A) means that we need to match the back of something.

5. (? <! A) does not mean that we need to match something, and (?! A) in the opposite direction

Examples:

1.(?=a):

 console.log ( "I am Chinese" .replace (/ I (? = Chinese) /, "rr"))

Print out: rr Chinese people    (the match is in front of the Chinese 'I am')

2.(?!a):

console.log ( "I am Chinese" .replace (/ (?! Chinese) /, "rr"))

Print out: rr I am Chinese  

3.(?:a):

 console.log ( "I am Chinese" .replace (/ (?: China) /, "rr"))

Print out: I am a man rr

4..(?<=a):

console.log ( "I am Chinese" .replace (/ (? <= Chinese) people /, "rr")) 

Print out: I am a Chinese rr

5.(?<!a):

  console.log ( "I am Chinese" .replace (/ (? <! China) /, "rr")) 

Print out: rr I am Chinese

 

 

Some regular practical use

Removal of Chinese string

console.log ( "aaa I am Chinese 111" .replace (/ [^ u4E00-u9FA5] / G, "")) // remove the Chinese, output: 'aaa111'

Remove the string English

console.log (? "aaa I am Chinese 111" .replace (/ ([az]) + / G, "")) // remove the English, Output: 'I am Chinese 111'

Removing the numeric string

console.log (? "aaa I am Chinese 111" .replace (/ (d) + / G, "")) // remove the digital output: 'aaa I am Chinese'

Digital Format

console.log("1234567890".replace(/B(?=(?:d{3})+(?!d))/g,",")) 
// 输出:'1,234,567,890'

Remove ip address

console.log("192.168.0.1".replace(/((2[0-4]d|25[0-5]|[01]?dd?).){3}(2[0-4]d|25[0-5]|[01]?dd?)/,"rr"))
// 输出:'rr'

 

 

 Reference links https://juejin.im/post/5ceb7d9df265da1b8811ba7f

Guess you like

Origin www.cnblogs.com/lwming/p/10943193.html