### 深入浅析正则表达式 捕获组

1: () 捕获组
2: (&#63;:) non capturing group
5: (?<=) positive lookbehind
6: (?<!) negative lookbehind
7: (?=), (?!), (?<=), (?<!)的捕获

1: () 捕获组

/go+/

/(go)+/

` let reg = /(go)+/ig; 'go is g gogo'.match(reg); //["go", "gogo"]`

` let reg = /(/d{2}).(/d{2}).(/d{4})/; let originString = '10.25.2017'; reg.test(originString); //true RegExp.\$1; //10 RegExp.\$2; //25 RegExp.\$2; //2017`

` let reg = /(/d{2}).(/d{2}).(/d{4})/; let originString = '10.25.2017'; let newString = originString.replace(reg, '\$3-\$1-\$2'); console.LOG(newString);//"2017-10-25"`

2: (?:) non capturing group 非捕获型分组

` let reg = /(?:/d{2}).(/d{2}).(/d{4})/; let originString = '10.25.2017'; reg.test(originString); //true RegExp.\$1; //25 RegExp.\$2; //2017 originString.match(reg);// ["10.25.2017", "25", "2017", index: 0, input: "10.25.2017", groups: undefined]`

` let reg = //d+(?=)/g; let reg1 = //d+/g; let str = '1 apple costs 10'; str.match(reg); //["10"] str.match(reg1); //["1", "10"]`

/x(?=y)/ 匹配x, 但是必须在x的【后面】【是】y的情况下

/x(?!y)/ 匹配x, 但是必须在x的【后面】【不是】y的情况下

` let reg = //d+(?!)/g; let str = '1 apple costs 2'; str.match(reg); ['1']`

5: (?<=) positive lookbehind 正向后顾型捕获

/(?<=y)x/ 匹配x, 但是只在【前面】【有】y的情况下

` let str = "1 turkey costs \$2"; console.log( str.match(/(?<=/\$)/d+/g) ); //["2"]`

6: (?<!) negative lookbehind 负向后顾型捕获

/(?<=y)x/ 匹配x, 但是只在【前面】【没有】y的情况下

` let str = "1 turkey costs \$2"; console.log( str.match(/(?<!/\$)/d+/g) ); //['1']`

7: (?=), (?!), (?<=), (?<!)的捕获

` let str = "1 turkey costs 2"; let reg = //d+(?=())/;  str.match(reg); //["2", "", index: 15, input: "1 turkey costs 2", groups: undefined]`

` let str = "1 turkey costs \$2"; let reg = /(?<=(/\$|£))/d+/; console.log( str.match(reg) ); //["2", "\$", index: 16, input: "1 turkey costs \$2", groups: undefined]`