Created
June 14, 2021 21:56
-
-
Save eduardorangell/ecd654b850818bee00113c1d78d19493 to your computer and use it in GitHub Desktop.
Replacing Special Characters in JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(/(^-+|-+$)/, ''); // Remove extra hyphens from beginning or end of the string | |
} | |
console.log(replaceSpecialChars('This is a sentence!!!')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment