Home >Backend Development >PHP Tutorial >The ambiguity of parentheses in regular expressions. Regular expressions. Remove parentheses. Regular expressions. Matching parentheses. Regular expressions. Matching parentheses.

The ambiguity of parentheses in regular expressions. Regular expressions. Remove parentheses. Regular expressions. Matching parentheses. Regular expressions. Matching parentheses.

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-29 08:48:351372browse

Parentheses in regular expressions have the following meanings

  • Limit the scope of the quantifier
  • Limit the scope of the multi-select structure
  • Capture text for back references
  • Group capture
  • Only grouping but not capturing
  • Look ahead

1. Limit the scope of quantifiers

1

2

3

varreg1 = /(Matz)?/; // 0 or 1 Matz

var reg2 = /(Matz)+/; // 1 or more Matz

var reg3 = /(Matz)*/; // 0 or more Matz

2. Limit the range of the multi-select structure

1

2

3

4

varreg = /(Matz|Eich)/

reg.test('Matz') // => true

reg.test('Eich') // => true

reg.test('John') // => false

3. Capture text for backreferences

1

2

3

4

5

6

varreg = /(boy)1/ // Equivalent to /boyboy/

reg.test('boy') // => false

reg.test('boyboy') // => true

var reg /(boy)(girl)12/

reg.test('boygirlboygirl') // => true

Four. Group capture

1

2

3

4

5

6

7

8

9

10

11

12

13

14

var reg1 = /(d{3}) (d{3})/

var str = '111 222'

str.replace(reg1, '$2 $1')// => '222 111' , note that $2 and $1 here store the matching string

var reg2 = /(d{3})(d{4})(d{4})/

var mobile = '13522722724'

reg2.test(mobile)

RegExp.$1 // => 135

RegExp.$2 // => 2272

RegExp.$3 // => 2724

var reg3 = /(d{3})(d{4})(d{4})/

var mobile = '13522722724'

mobile.replace(reg3, '$1 $2 $3')// => '135 2272 2724'

5. Only grouping without capturing (together with "?:")

1

2

3

var reg = /(?:d+)/

reg.test('13522722724')

RegExp.$1 // => '' Do not store matched elements

In longer regular expressions, back references will reduce the matching speed and performance. Grouping without capturing should be used when back references are not needed. .

6. Lookahead (with "?=")

It tells the regular expression to look forward some characters but not move the position. Lookahead does not match any characters and only matches specific positions in the text.

1

2

3

4

5

var reg = /(John) (?=Resig)/

reg.test('John') // => false

reg.test('John Backus')// => false

reg.test('John Reisg')// => true

RegExp.$1 // => 'John', please note that this is not "John Resig"

The following is a small function that uses lookahead to format mobile phone numbers

1

2

3

4

5

6

7

8

9

/*

* Separate mobile phone numbers

* 13522722724 -> 135 2272 2724

*/

function separateMobile(num) {

var arr = ('0'+ num ).replace(/(d{4})(?=d)/g,"$1 ").split('')

arr.shift()

return arr.join('' )

}

The above introduces the ambiguity of regular expression parentheses, including regular expressions and parentheses. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn