1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
import MultiRegexp from 'multi-regexp2'
export function createMultiRegexp(regexString: string) {
let insideCharClass = false
let captureGroupIndex = -1
for (let i = 0; i < regexString.length; i++) {
if (
!insideCharClass &&
regexString[i] === '[' &&
regexString[i - 1] !== '\\'
) {
insideCharClass = true
} else if (
insideCharClass &&
regexString[i] === ']' &&
regexString[i - 1] !== '\\'
) {
insideCharClass = false
} else if (
!insideCharClass &&
regexString[i] === '(' &&
regexString.substr(i + 1, 2) !== '?:'
) {
captureGroupIndex = i
break
}
}
const re = /(?:[^\\]|^)\(\?:/g
let match: RegExpExecArray
let nonCaptureGroupIndexes: number[] = []
while ((match = re.exec(regexString)) !== null) {
if (match[0].startsWith('(')) {
nonCaptureGroupIndexes.push(match.index)
} else {
nonCaptureGroupIndexes.push(match.index + 1)
}
}
const regex = new MultiRegexp(
new RegExp(
regexString.replace(re, (m) => m.substr(0, m.length - 2)),
'g'
)
)
let groupIndex =
1 + nonCaptureGroupIndexes.filter((i) => i < captureGroupIndex).length
return {
exec: (str: string) => {
return regex.execForGroup(str, groupIndex)
},
}
}
|