zondag 13 april 2014

Replace all diacritics in strings using a Regular Expression

To replace all diacritics (aka 'extended characters') by their HtmlEncoded counterpart in strings use:
[SomeString]
  .replace(
           /[\u0080-\u024F]/g,
           function(a) {return '&#'+a.charCodeAt(0)+';';}
  );
For example:
"Diëlectrische Coëfficiënten".replace(/[\u0080-\u024F]/g, function(a) {return '&#'+a.charCodeAt(0)+';';});
//    returns
//=> "Diëlectrische Coëfficiënten"


Geen opmerkingen: