admin管理员组文章数量:1334183
I need to treat accented characters as if they were the same as their non accented counterparts. This is my code:
var re = new RegExp(string, 'i');
if(target.search(re) == 0) { }
It currently ignores the character's case, how do I also ignore if the character is accented or not?
I need to treat accented characters as if they were the same as their non accented counterparts. This is my code:
var re = new RegExp(string, 'i');
if(target.search(re) == 0) { }
It currently ignores the character's case, how do I also ignore if the character is accented or not?
Share Improve this question asked Jun 20, 2012 at 8:16 lisovaccarolisovaccaro 34k99 gold badges270 silver badges423 bronze badges 1- 3 Try removing the accents from the string first and then passing a regex. stackoverflow./questions/990904/…. In any case that should work. This question or similar has been asked many times. – elclanrs Commented Jun 20, 2012 at 8:23
2 Answers
Reset to default 5I think you have to remove the accents first then do your RegExp.
You can use this function taht I found here :
function stripVowelAccent(str)
{
var rExps=[
{re:/[\xC0-\xC6]/g, ch:'A'},
{re:/[\xE0-\xE6]/g, ch:'a'},
{re:/[\xC8-\xCB]/g, ch:'E'},
{re:/[\xE8-\xEB]/g, ch:'e'},
{re:/[\xCC-\xCF]/g, ch:'I'},
{re:/[\xEC-\xEF]/g, ch:'i'},
{re:/[\xD2-\xD6]/g, ch:'O'},
{re:/[\xF2-\xF6]/g, ch:'o'},
{re:/[\xD9-\xDC]/g, ch:'U'},
{re:/[\xF9-\xFC]/g, ch:'u'},
{re:/[\xD1]/g, ch:'N'},
{re:/[\xF1]/g, ch:'n'} ];
for(var i=0, len=rExps.length; i<len; i++)
str=str.replace(rExps[i].re, rExps[i].ch);
return str;
}
uses the library semplice
http://semplicewebsites./removing-accents-javascript
var latin_map = {
'Á': 'A', // LATIN CAPITAL LETTER A WITH ACUTE
'Ă': 'A', // LATIN CAPITAL LETTER A WITH BREVE
...
'ᵥ': 'v', // LATIN SUBSCRIPT SMALL LETTER V
'ₓ': 'x', // LATIN SUBSCRIPT SMALL LETTER X
};
String.prototype.latinise = function() {
return this.replace(/[^A-Za-z0-9]/g, function(x) { return latin_map[x] || x; })
};
本文标签: javascriptstringsearch() that ignores accented charactersStack Overflow
版权声明:本文标题:javascript - string.search() that ignores accented characters? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742309972a2450670.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论