Skip to content

Commit 95320d5

Browse files
committed
add non-capturing group
1 parent ab3536c commit 95320d5

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

src/__tests__/unit/checks/keywords-urls.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ describe('keywords guardrail', () => {
8080
const result = await keywordsCheck({}, '你好12345', KeywordsConfig.parse({ keywords: ['你好123'] }));
8181
expect(result.tripwireTriggered).toEqual(false);
8282
});
83+
84+
it('should apply word boundaries to all keywords in a multi-keyword pattern', async () => {
85+
const result = await keywordsCheck({}, 'testing hello world', KeywordsConfig.parse({ keywords: ['test', 'hello', 'world'] }));
86+
expect(result.tripwireTriggered).toEqual(true);
87+
expect(result.info.matchedKeywords).toEqual(['hello', 'world']);
88+
});
8389
});
8490

8591
describe('urls guardrail', () => {

src/checks/keywords.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export const keywordsCheck: CheckFn<KeywordsContext, string, KeywordsConfig> = (
6161
// \p{L}|\p{N}|_ - any unicode letter, number, or underscore. Alternative to \b
6262
// (?<!\p{L}) - not preceded by a letter
6363
// (?!\p{L}) - not followed by a letter
64-
const patternText = `(?<!\\p{L}|\\p{N}|_)${escapedKeywords.join('|')}(?!\\p{L}|\\p{N}|_)`;
64+
const patternText = `(?<!\\p{L}|\\p{N}|_)(?:${escapedKeywords.join('|')})(?!\\p{L}|\\p{N}|_)`;
6565
const pattern = new RegExp(patternText, 'giu'); // case-insensitive, global, unicode
6666

6767
const matches: string[] = [];

0 commit comments

Comments
 (0)