Fix Thai search keywords not properly hi-lighted in the search results. (#8347)

This commit is contained in:
Pattara Kiatisevi 2024-12-04 01:40:07 +07:00 committed by GitHub
parent 7f21754b78
commit 21e929dca2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 8 additions and 3 deletions

View file

@ -185,6 +185,11 @@ describe('Utility functions', () => {
expect(result.pattern).toEqual(/()(你好)/gi);
});
it('should create regex for Thai characters', () => {
const result = convertSearchTermToRegex('สวัสดี');
expect(result.pattern).toEqual(/()(สวัสดี)/gi);
});
it('should create regex for wildcard at the end', () => {
const result = convertSearchTermToRegex('hello*');
expect(result.pattern).toEqual(/\b()(hello)/gi);

View file

@ -18,9 +18,9 @@ type LanguageObject = {
};
}
// pattern to detect the existence of a Chinese, Japanese, or Korean character in a string
// pattern to detect the existence of a Chinese, Japanese, Korean, or Thai character in a string
// http://stackoverflow.com/questions/15033196/using-javascript-to-check-whether-a-string-contains-japanese-characters-includi
const cjkPattern = /[\u3000-\u303f\u3040-\u309f\u30a0-\u30ff\uff00-\uff9f\u4e00-\u9faf\u3400-\u4dbf\uac00-\ud7a3]/;
const cjkPattern = /[\u3000-\u303f\u3040-\u309f\u30a0-\u30ff\uff00-\uff9f\u4e00-\u9faf\u3400-\u4dbf\uac00-\ud7a3\u0e00-\u0e7f]/;
const puncStart = /^[^\p{L}\d\s#]+/u;
const puncEnd = /[^\p{L}\d\s]+$/u;
@ -347,7 +347,7 @@ export function convertSearchTermToRegex(term: string): SearchPattern {
let pattern;
if (cjkPattern.test(term)) {
// term contains Chinese, Japanese, or Korean characters so don't mark word boundaries
// term contains Chinese, Japanese, Korean or Thai characters so don't mark word boundaries
pattern = '()(' + escapeRegex(term.replace(/\*/g, '')) + ')';
} else if ((/[^\s][*]$/).test(term)) {
pattern = '\\b()(' + escapeRegex(term.substring(0, term.length - 1)) + ')';