JavaScript Regexp accent insensitive and uppercase insensitive? [duplicate]

问题: This question already has answers here: How to ignore acute accent in a javascript regex match?...

问题:

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 !


回答1:

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"));

See documentation on sensitivity and other options here


回答2:

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.

  • 发表于 2020-06-27 22:55
  • 阅读 ( 78 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除