Skip to content

Instantly share code, notes, and snippets.

@eduardorangell
Created June 14, 2021 21:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eduardorangell/ecd654b850818bee00113c1d78d19493 to your computer and use it in GitHub Desktop.
Save eduardorangell/ecd654b850818bee00113c1d78d19493 to your computer and use it in GitHub Desktop.
Replacing Special Characters in JavaScript
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