diff --git a/src/utilities/wordBoundariesRegex.ts b/src/utilities/wordBoundariesRegex.ts index 9ccefb795..f47545571 100644 --- a/src/utilities/wordBoundariesRegex.ts +++ b/src/utilities/wordBoundariesRegex.ts @@ -4,9 +4,9 @@ export default (input: string): RegExp => { // Regex word boundaries that work for cyrillic characters - https://stackoverflow.com/a/47062016/1717697 const wordBoundaryBefore = '(?:(?:[^\\p{L}\\p{N}])|^)'; // Converted to a non-matching group instead of positive lookbehind for Safari const wordBoundaryAfter = '(?=[^\\p{L}\\p{N}]|$)'; - const regex = words.reduce((pattern, word, i) => { - return `${pattern}(?=.*${wordBoundaryBefore}${word}.*${wordBoundaryAfter})${i + 1 === words.length ? '.+' : ''}`; + const escapedWord = word.replace(/[\\^$*+?\\.()|[\]{}]/g, '\\$&'); + return `${pattern}(?=.*${wordBoundaryBefore}.*${escapedWord}.*${wordBoundaryAfter})${i + 1 === words.length ? '.+' : ''}`; }, ''); return new RegExp(regex, 'i'); }; diff --git a/test/collections-rest/int.spec.ts b/test/collections-rest/int.spec.ts index fffe890fb..3a92083d8 100644 --- a/test/collections-rest/int.spec.ts +++ b/test/collections-rest/int.spec.ts @@ -373,6 +373,31 @@ describe('collections-rest', () => { expect(result.totalDocs).toEqual(1); }); + + describe('like - special characters', () => { + const specialCharacters = '~!@#$%^&*()_+-+[]{}|;:"<>,.?/})'; + + it.each(specialCharacters.split(''))('like - special characters - %s', async (character) => { + const post1 = await createPost({ + title: specialCharacters, + }); + + const query = { + query: { + title: { + like: character, + }, + }, + }; + + const { status, result } = await client.find(query); + + expect(status).toEqual(200); + expect(result.docs).toEqual([post1]); + expect(result.totalDocs).toEqual(1); + }); + }); + it('like - cyrillic characters', async () => { const post1 = await createPost({ title: 'Тест' });