#Create a search removing accents or special characters

3 messages · Page 1 of 1 (latest)

marble roost
#

Hi everyone,

I have a content with title "Vício", but I can't find it if I search by "vicio" (without the accent).

My query

or: [
            {
                title: {
                    contains: search,
                },
            },
            {
                indexableContent: {
                    contains: search,
                }
            },
        ]

Which approach should I use to prevent this in the API layer? Any help is welcome

fresh islandBOT
tame gorge
#

using this helper function you can normalize anything with an accent or caps to the plain base word, first part just decomposes the accent (splits the accent as its own character without affectingthe original word so vício becomes viócio) the second part removes the accent characters thus we remain with vicio :

TL;DR We strip away the accent

function normalize(str: string) {
return str.normalize("NFD").replace(/[\u0300-\u036f]/g, "");
}