Skip to content

Commit 57b0978

Browse files
committed
Merge branch 'fix/regex-capture-error' into chore/all-my-stuffs
2 parents bc50a87 + 11c0ac7 commit 57b0978

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

src/tools/regex-tester/regex-tester.service.test.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,51 @@ const regexesData = [
9292
},
9393
],
9494
},
95+
{
96+
regex: '\\s([^\\s\\[]+)(?:\\[(\\d+)\\])?:\\s',
97+
text: 'Nov 11 21:03:26 abc2 def.sh[1]: \nNov 11 21:03:26 abc2 def.sh: ',
98+
flags: 'gm',
99+
result: [
100+
{
101+
captures: [
102+
{
103+
end: 27,
104+
name: '1',
105+
start: 21,
106+
value: 'def.sh',
107+
},
108+
{
109+
end: 29,
110+
name: '2',
111+
start: 28,
112+
value: '1',
113+
},
114+
],
115+
groups: [],
116+
index: 20,
117+
value: ' def.sh[1]: ',
118+
},
119+
{
120+
captures: [
121+
{
122+
end: 60,
123+
name: '1',
124+
start: 54,
125+
value: 'def.sh',
126+
},
127+
{
128+
end: -1,
129+
name: '2',
130+
start: -1,
131+
value: undefined,
132+
},
133+
],
134+
groups: [],
135+
index: 53,
136+
value: ' def.sh: ',
137+
},
138+
],
139+
},
95140
];
96141

97142
describe('regex-tester', () => {

src/tools/regex-tester/regex-tester.service.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,23 @@ export function matchRegex(regex: string, text: string, flags: string) {
3131
const captures: Array<GroupCapture> = [];
3232
Object.entries(match).forEach(([captureName, captureValue]) => {
3333
if (captureName !== '0' && captureName.match(/\d+/)) {
34+
const captureIndices = indices[Number(captureName)] || [-1, -1];
3435
captures.push({
3536
name: captureName,
3637
value: captureValue,
37-
start: indices[Number(captureName)][0],
38-
end: indices[Number(captureName)][1],
38+
start: captureIndices[0],
39+
end: captureIndices[1],
3940
});
4041
}
4142
});
4243
const groups: Array<GroupCapture> = [];
4344
Object.entries(match.groups || {}).forEach(([groupName, groupValue]) => {
45+
const groupIndices = indices.groups[groupName] || [-1, -1];
4446
groups.push({
4547
name: groupName,
4648
value: groupValue,
47-
start: indices.groups[groupName][0],
48-
end: indices.groups[groupName][1],
49+
start: groupIndices[0],
50+
end: groupIndices[1],
4951
});
5052
});
5153
results.push({

0 commit comments

Comments
 (0)