@@ -40,12 +40,14 @@ export type KeywordsContext = z.infer<typeof KeywordsContext>;
4040 * @param config Configuration specifying keywords and behavior
4141 * @returns GuardrailResult indicating if tripwire was triggered
4242 */
43- const unicodeWordCharRegex = / [ \p{ L} \p{ N} ] / u;
44- const isWordChar = ( char : string | undefined ) : boolean => {
45- if ( ! char ) return false ;
46- if ( char === '_' ) return true ;
47- return unicodeWordCharRegex . test ( char ) ;
48- } ;
43+ const WORD_CHAR_CLASS = '[\\p{L}\\p{N}_]' ;
44+ const isWordChar = ( ( ) => {
45+ const wordCharRegex = new RegExp ( WORD_CHAR_CLASS , 'u' ) ;
46+ return ( char : string | undefined ) : boolean => {
47+ if ( ! char ) return false ;
48+ return wordCharRegex . test ( char ) ;
49+ } ;
50+ } ) ( ) ;
4951
5052export const keywordsCheck : CheckFn < KeywordsContext , string , KeywordsConfig > = (
5153 ctx ,
@@ -86,8 +88,8 @@ export const keywordsCheck: CheckFn<KeywordsContext, string, KeywordsConfig> = (
8688 const lastChar = keywordChars [ keywordChars . length - 1 ] ;
8789 const needsLeftBoundary = isWordChar ( firstChar ) ;
8890 const needsRightBoundary = isWordChar ( lastChar ) ;
89- const leftBoundary = needsLeftBoundary ? ' (?<![\\p{L}\\p{N}_])' : '' ;
90- const rightBoundary = needsRightBoundary ? ' (?![\\p{L}\\p{N}_])' : '' ;
91+ const leftBoundary = needsLeftBoundary ? ` (?<!${ WORD_CHAR_CLASS } )` : '' ;
92+ const rightBoundary = needsRightBoundary ? ` (?!${ WORD_CHAR_CLASS } )` : '' ;
9193 return `${ leftBoundary } ${ escaped } ${ rightBoundary } ` ;
9294 } ) ;
9395
0 commit comments