Skip to content

Instantly share code, notes, and snippets.

Avatar
🍺
Earned: Hangover

Eduardo Rangell eduardorangell

🍺
Earned: Hangover
View GitHub Profile
@eduardorangell
eduardorangell / replacespecialchars.js
Created Jun 14, 2021
Replacing Special Characters in JavaScript
View replacespecialchars.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(/(^-+|-+$)/, ''); // Remove extra hyphens from beginning or end of the string
}
console.log(replaceSpecialChars('This is a sentence!!!'));