diff --git a/src/utils/regexp.ts b/src/utils/regexp.ts index 61d8226..f9b63db 100644 --- a/src/utils/regexp.ts +++ b/src/utils/regexp.ts @@ -1,4 +1,4 @@ -type Replacement = { mapName: string; exp: string; inverse: boolean }; +type Replacement = { order: number; mapName: string; exp: string; inverse: boolean }; /** * replace all parenthesis groups with placeholder @@ -17,7 +17,7 @@ const replaceParenthesisGroups = (input: string, replacements: Replacement[], nu const replaceExpression = (expression: string, group: string, inverse: boolean) => { const mapName = `##R${num}##`; - replacements.push({ mapName, exp: group, inverse }); + replacements.push({ mapName, exp: group, inverse, order: num }); replaced = replaced.replace(expression, mapName); return replaceParenthesisGroups(replaced, replacements, num + 1); @@ -74,7 +74,7 @@ export const selectionTestGrep = (str: string): RegExp => { // last group should be converted first groups - .sort((a, b) => (a.mapName > b.mapName ? -1 : 1)) + .sort((a, b) => (a.order > b.order ? -1 : 1)) .forEach(r => { convertedString = convertedString.replace(r.mapName, r.reg); }); diff --git a/tests/test-folder/regexp.test.ts b/tests/test-folder/regexp.test.ts index c3f3815..0e5b2b0 100644 --- a/tests/test-folder/regexp.test.ts +++ b/tests/test-folder/regexp.test.ts @@ -65,6 +65,19 @@ describe('suite', () => { { expectMatch: false, testLine: 'his test' }, ], }, + { + desc: 'parenthesis several more complex - many parent parenthesis', + GREP: '((((((.*)&!((my test)|(his test))&(.*))))))', + regExpected: /(?=.*.*)+(?=.*^(?!.*(my test|his test).*))+(?=.*.*)+.*/i, + cases: [ + { expectMatch: true, testLine: 'test her' }, + { expectMatch: true, testLine: 'her test' }, + { expectMatch: false, testLine: 'her his test' }, + { expectMatch: false, testLine: 'her my test' }, + { expectMatch: false, testLine: 'my test' }, + { expectMatch: false, testLine: 'his test' }, + ], + }, { desc: 'tag with dot encoded', GREP: '@test\\.1',