lisovaccaro
lisovaccaro

Reputation: 33956

string.search() that ignores accented characters?

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?

Upvotes: 1

Views: 5006

Answers (2)

Mark Pro Campos
Mark Pro Campos

Reputation: 364

uses the library semplice

http://semplicewebsites.com/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; })
};

Upvotes: 3

jbrtrnd
jbrtrnd

Reputation: 3833

I 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;
}

Upvotes: 5

Related Questions