@@ -103,35 +103,41 @@ const GlossaryInjector: React.FC<GlossaryInjectorProps> = ({ children }) => {
103
103
104
104
// For the Japanese version of the site, don't use word boundaries that don't work well with Japanese characters.
105
105
if ( isJapaneseSite ) {
106
+ // For Japanese text, we can't use word boundaries, so just match the exact term.
106
107
return `(${ escapedTerm } )` ;
107
108
}
108
109
109
110
// For English site, match exact term or term followed by 's' or 'es' at word boundary.
110
- return `(\\b${ escapedTerm } (s|es)? \\b)` ;
111
+ return `(\\b${ escapedTerm } \\b|\\b ${ escapedTerm } s\\b|\\b ${ escapedTerm } es \\b)` ;
111
112
} ) . join ( '|' ) ;
112
113
113
- const regex = new RegExp ( regexPattern , 'gi' ) ; // The 'i' flag is for case-insensitive matching.
114
+ const regex = new RegExp ( regexPattern , 'gi' ) ; // Use case-insensitive matching.
114
115
115
116
let lastIndex = 0 ;
116
117
let match : RegExpExecArray | null ;
117
118
118
119
while ( ( match = regex . exec ( currentText ) ) ) {
119
120
const matchedText = match [ 0 ] ; // The full matched text (may include plural suffix).
120
121
122
+ // For Japanese, remove any non-word characters that were captured by the regex.
123
+ const actualMatch = isJapaneseSite
124
+ ? matchedText . replace ( / ^ [ ^ \p{ L} \p{ N} _ ] + | [ ^ \p{ L} \p{ N} _ ] + $ / gu, '' )
125
+ : matchedText ;
126
+
121
127
// Find the base term from the glossary that matches.
122
128
let baseTerm : string | undefined ;
123
129
124
130
if ( isJapaneseSite ) {
125
131
// For Japanese, look for an exact match only.
126
132
baseTerm = terms . find ( term =>
127
- matchedText . toLowerCase ( ) === term . toLowerCase ( )
133
+ actualMatch === term
128
134
) ;
129
135
} else {
130
136
// For English, check both singular and plural forms too.
131
137
baseTerm = terms . find ( term =>
132
- matchedText . toLowerCase ( ) === term . toLowerCase ( ) ||
133
- matchedText . toLowerCase ( ) === `${ term . toLowerCase ( ) } s` ||
134
- matchedText . toLowerCase ( ) === `${ term . toLowerCase ( ) } es`
138
+ actualMatch . toLowerCase ( ) === term . toLowerCase ( ) ||
139
+ actualMatch . toLowerCase ( ) === `${ term . toLowerCase ( ) } s` ||
140
+ actualMatch . toLowerCase ( ) === `${ term . toLowerCase ( ) } es`
135
141
) ;
136
142
}
137
143
0 commit comments