Replacing diacritics in Javascript

In modern browsers and node.js you can use unicode normalization to decompose those characters followed by a filtering regex.

str.normalize('NFKD').replace(/[^\w]/g, '')

If you wanted to allow characters such as whitespaces, dashes, etc. you should extend the regex to allow them.

str.normalize('NFKD').replace(/[^\w\s.-_\/]/g, '')

var str="áàâäãéèëêíìïîóòöôõúùüûñçăşţ";
var asciiStr = str.normalize('NFKD').replace(/[^\w]/g, '');
console.info(str, asciiStr);

NOTES: This method does not work with characters that do not have unicode composed varian. i.e. ø and ł

Leave a Comment