使用javascript执行查找/匹配,忽略特殊语言字符(例如,重音符号)?

Ric*_*uez 1 javascript jquery

使用javascript中的stripos函数,例如:

function stripos (f_haystack, f_needle, f_offset) {
  var haystack = (f_haystack + '').toLowerCase();
  var needle = (f_needle + '').toLowerCase();
  var index = 0;

  if ((index = haystack.indexOf(needle, f_offset)) !== -1) {
    return index;
  }
  return false;
}
Run Code Online (Sandbox Code Playgroud)

我如何使用/重新编码此函数以使其与特殊字符匹配?

仿佛:

var haystack = 'Le créme';
var needle   = 'reme';
// ^ these two should match (anything other than false)
Run Code Online (Sandbox Code Playgroud)

Lua*_*tro 8

你可以在搜索之前清理字符串

String.prototype.removeAccents = function(){
 return this
         .replace(/[áàãâä]/gi,"a")
         .replace(/[éè¨ê]/gi,"e")
         .replace(/[íìïî]/gi,"i")
         .replace(/[óòöôõ]/gi,"o")
         .replace(/[úùüû]/gi, "u")
         .replace(/[ç]/gi, "c")
         .replace(/[ñ]/gi, "n")
         .replace(/[^a-zA-Z0-9]/g," ");
}
Run Code Online (Sandbox Code Playgroud)

使用:

function stripos (f_haystack, f_needle, f_offset) {
  var haystack = (f_haystack + '').toLowerCase().removeAccents();
  var needle = (f_needle + '').toLowerCase().removeAccents();
  var index = 0;

  if ((index = haystack.indexOf(needle, f_offset)) !== -1) {
    return index;
  }
  return false;
}
Run Code Online (Sandbox Code Playgroud)