I have some records in database that are containing Serbian latin letters (ć, č, đ, ž, š).
I've done search functionality in backend (nestjs) to search all columns in table that contain typed characters on frontend (react).
How could I search these latin letters when typed character is for example c (this should look for all 3 characters with same "base" -> c, č, ć). Same should be done with the others.
I tried some regex, but never got desired result.
Any basic ideas how should I do this?
CodePudding user response:
Since you have a postgres
tag, this can be done using SIMILAR
You only need to construct your right part, inside %%
so as to include these variations
select * from yourtable
where yourcolumn SIMILAR TO '%(c|ć|č)%'
CodePudding user response:
you can do something like this
const string = 'this is normal but this ć, č, đ, ž, š not'
const normalizeMap = {
'č': 'c',
'ć': 'c',
'đ': 'd',
'š': 's',
'ž': 'z'
}
const normalize = string => Object
.entries(normalizeMap)
.reduce((res, [raw, normalized]) => res.replace(new RegExp(raw, 'g'), normalized), string)
console.log(normalize(string))
CodePudding user response:
You can probably utilize localCompare
for this.
The localeCompare() method returns a number indicating whether a reference string comes before, or after, or is the same as the given string in sort order. In implementations with Intl.Collator API support, this method simply calls Intl.Collator.
A negative number if referenceStr occurs before compareString; positive if the referenceStr occurs after compareString; 0 if they are equivalent.
An example:
let a = 'caknut';
let b = 'ćaknut';
console.log(a.localeCompare(b, 'sr', { sensitivity: 'base' })); // 0
A 0
indicates that both strings are equivalent.