Here my code :
this.datas = this.datas.filter(item => new RegExp(val, 'i').test(item.name));
For example, if i search "créaTION" i need to get all "creation" "création" "Creation". Anyone know how to do this ? Thank you !
Here my code :
this.datas = this.datas.filter(item => new RegExp(val, 'i').test(item.name));
For example, if i search "créaTION" i need to get all "creation" "création" "Creation". Anyone know how to do this ? Thank you !
Anyone know how to do this ?
Yes, it's extremely easy. First step is to not use regular expressions. Just use String#localeCompare
which can take a third parameter with options. If you supply a sensitivity: "base"
as property there, that will take care of all diacritics and difference cases for letters.
const datas = [
"creation",
"Creation",
"creátion",
"créåtiön",
"CRÉÅTIÖN",
"foo",
"bar"
]
function search(val) {
return datas.filter(
//if localeCompare returns `0` then there is no difference in the words. They match
item => item.localeCompare(val, undefined, { sensitivity: "base"}) === 0
)
}
console.log(search("créaTION"));
console.log(search("creation"));
Or closer to your case:
const obj = {
datas: [
{ name: "creation" },
{ name: "Creation" },
{ name: "creátion" },
{ name: "créåtiön" },
{ name: "CRÉÅTIÖN" },
{ name: "foo" },
{ name: "bar" }
],
search(val) {
return this.datas
.filter(
//if localeCompare returns `0` then there is no difference in the words. They match
item => item.name.localeCompare(val, undefined, { sensitivity: "base"}) === 0
)
.map(item => item.name);
}
}
console.log(obj.search("créaTION"));
console.log(obj.search("creation"));
It can be done with the character range expression of JavaScripts regular expressions:
var val = 'cr[ée]ation';
// Your code modified. Just a prove that it works:
var data = ['no match', 'créaTION', 'creation', 'création', 'Creation'];
var regExp = new RegExp('cr[ée]ation', 'i');
data = data.filter(item => regExp.test(item));
console.log(data);
You may also need to include the uppercase E with accent: 'cr[eéÉ]ation'
in the expression, because so called "local characters" may not supported by all browsers jet.
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!