- Jul 2022
-
ricardometring.com ricardometring.com
-
Here's a complete function that removes accents, replaces special characters with hyphens, also removing additional hyphens:
```js const replaceSpecialChars = (str) => { return str.normalize('NFD').replace(/[\u0300-\u036f]/g, '') // Remove accents .replace(/([^\w]+|\s+)/g, '-') // Replace space and other characters by hyphen .replace(/--+/g, '-') // Replaces multiple hyphens by one hyphen .replace(/(^-+|-+$)/g, ''); // Remove extra hyphens from beginning or end of the string }
console.log(replaceSpecialChars('This is a sentence!!!')); ```
-
-
stackoverflow.com stackoverflow.com
-
```js function replaceDiacritics(s) { var s;
var diacritics =[ /[\300-\306]/g, /[\340-\346]/g, // A, a /[\310-\313]/g, /[\350-\353]/g, // E, e /[\314-\317]/g, /[\354-\357]/g, // I, i /[\322-\330]/g, /[\362-\370]/g, // O, o /[\331-\334]/g, /[\371-\374]/g, // U, u /[\321]/g, /[\361]/g, // N, n /[\307]/g, /[\347]/g, // C, c ];
var chars = ['A','a','E','e','I','i','O','o','U','u','N','n','C','c'];
for (var i = 0; i < diacritics.length; i++) { s = s.replace(diacritics[i],chars[i]); }
document.write(s); } ```
-
-
developer.mozilla.org developer.mozilla.org