Skip to content

Commit 9246e0c

Browse files
authored
[patch] fix defect - no matches when many parenthesis groups (#25)
1 parent 25a5b97 commit 9246e0c

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

src/utils/regexp.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
type Replacement = { mapName: string; exp: string; inverse: boolean };
1+
type Replacement = { order: number; mapName: string; exp: string; inverse: boolean };
22

33
/**
44
* replace all parenthesis groups with placeholder
@@ -17,7 +17,7 @@ const replaceParenthesisGroups = (input: string, replacements: Replacement[], nu
1717

1818
const replaceExpression = (expression: string, group: string, inverse: boolean) => {
1919
const mapName = `##R${num}##`;
20-
replacements.push({ mapName, exp: group, inverse });
20+
replacements.push({ mapName, exp: group, inverse, order: num });
2121
replaced = replaced.replace(expression, mapName);
2222

2323
return replaceParenthesisGroups(replaced, replacements, num + 1);
@@ -74,7 +74,7 @@ export const selectionTestGrep = (str: string): RegExp => {
7474

7575
// last group should be converted first
7676
groups
77-
.sort((a, b) => (a.mapName > b.mapName ? -1 : 1))
77+
.sort((a, b) => (a.order > b.order ? -1 : 1))
7878
.forEach(r => {
7979
convertedString = convertedString.replace(r.mapName, r.reg);
8080
});

tests/test-folder/regexp.test.ts

+13
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,19 @@ describe('suite', () => {
6565
{ expectMatch: false, testLine: 'his test' },
6666
],
6767
},
68+
{
69+
desc: 'parenthesis several more complex - many parent parenthesis',
70+
GREP: '((((((.*)&!((my test)|(his test))&(.*))))))',
71+
regExpected: /(?=.*.*)+(?=.*^(?!.*(my test|his test).*))+(?=.*.*)+.*/i,
72+
cases: [
73+
{ expectMatch: true, testLine: 'test her' },
74+
{ expectMatch: true, testLine: 'her test' },
75+
{ expectMatch: false, testLine: 'her his test' },
76+
{ expectMatch: false, testLine: 'her my test' },
77+
{ expectMatch: false, testLine: 'my test' },
78+
{ expectMatch: false, testLine: 'his test' },
79+
],
80+
},
6881
{
6982
desc: 'tag with dot encoded',
7083
GREP: '@test\\.1',

0 commit comments

Comments
 (0)