Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
An Javascript function to remove accents and others characters from an input string.
// Example: https://codepen.io/marcelo-ribeiro/pen/OJmVOyW
const accentsMap = new Map([
["A", "Á|À|Ã|Â|Ä"],
["a", "á|à|ã|â|ä"],
["E", "É|È|Ê|Ë"],
["e", "é|è|ê|ë"],
["I", "Í|Ì|Î|Ï"],
["i", "í|ì|î|ï"],
["O", "Ó|Ò|Ô|Õ|Ö"],
["o", "ó|ò|ô|õ|ö"],
["U", "Ú|Ù|Û|Ü"],
["u", "ú|ù|û|ü"],
["C", "Ç"],
["c", "ç"],
["N", "Ñ"],
["n", "ñ"]
]);
const reducer = (acc, [key]) => acc.replace(new RegExp(accentsMap.get(key), "g"), key);
export const removeAccents = (text) => [...accentsMap].reduce(reducer, text);
export const slugify = text => text
.replace(/\s|_|\(|\)/g, "-")
.normalize("NFD").replace(/\p{Diacritic}/gu, "")
.toLowerCase()
// Example: https://codepen.io/marcelo-ribeiro/pen/PomqOvE
const accentsMap = new Map([
["-", "\\s|\\.|_"],
["a", "á|à|ã|â|ä"],
["e", "é|è|ê|ë"],
["i", "í|ì|î|ï"],
["o", "ó|ò|ô|õ|ö"],
["u", "ú|ù|û|ü"],
["c", "ç"],
["n", "ñ"]
]);
const reducer = (acc, [key]) => acc.replace(new RegExp(accentsMap.get(key), "gi"), key);
export const slugify = (text) => [...accentsMap].reduce(reducer, text.toLowerCase());
export const removeAccents = text => text.normalize("NFD").replace(/\p{Diacritic}/gu, "")
@aelfannir
Copy link

aelfannir commented Jun 26, 2019

Thank you !

@MrVincs
Copy link

MrVincs commented Dec 20, 2019

Amazing ! thank you

@Bohemme
Copy link

Bohemme commented Mar 25, 2020

Great! I'm using it in a Isotope sorting function and works like a charm. Thanks!

@torchatlas
Copy link

torchatlas commented Apr 2, 2020

Just writing another review for this code: it works like a charm! Not sure about performance, but it is working well! Thanks for posting this!

@WeslleyBorges
Copy link

WeslleyBorges commented Apr 4, 2020

God!!

@alectrocute
Copy link

alectrocute commented May 16, 2020

It's beautiful

@ahmedhijazi94
Copy link

ahmedhijazi94 commented Jun 2, 2020

valeu, lindão!

@oaugusto256
Copy link

oaugusto256 commented Jul 24, 2020

Naisu!

@arenier
Copy link

arenier commented Jul 28, 2020

Thanks a lot for this snippet.
I improved for ES6 use, in case anyone might want it:

const accentsMap = {
  a: 'á|à|ã|â|À|Á|Ã|Â',
  e: 'é|è|ê|É|È|Ê',
  i: 'í|ì|î|Í|Ì|Î',
  o: 'ó|ò|ô|õ|Ó|Ò|Ô|Õ',
  u: 'ú|ù|û|ü|Ú|Ù|Û|Ü',
  c: 'ç|Ç',
  n: 'ñ|Ñ',
};

export const slugify = text => Object.keys(accentsMap).reduce((acc, cur) => acc.replace(new RegExp(accentsMap[cur], 'g'), cur), text);

@lucasbpro
Copy link

lucasbpro commented Aug 26, 2020

Valeu por compartilhar!

@Yann-BUTSCHER-EIRL
Copy link

Yann-BUTSCHER-EIRL commented Sep 1, 2020

Thanks a lot!

@cauefcr
Copy link

cauefcr commented Sep 24, 2020

salvou meu dia!

@SlyKode
Copy link

SlyKode commented Nov 18, 2020

Thank you for sharing. Note that ä,Ä,ë,Ë,ï,Ï,ö,Ö are missing in the map.

@Sami69
Copy link

Sami69 commented Dec 18, 2020

I have modified the last code, that I find really nice, to remove all none alpha-numeric values. In names and firstname you often have characters such as '(', ')' and other character types. I needed this to make a filter search of similar names and firstname in a database for subscription. It makes t easuer to compasre with other names in the database:

const accentsMap = {
  a: 'á|à|ã|â|À|Á|Ã|Â',
  e: 'é|è|ê|É|È|Ê',
  i: 'í|ì|î|Í|Ì|Î',
  o: 'ó|ò|ô|õ|Ó|Ò|Ô|Õ',
  u: 'ú|ù|û|ü|Ú|Ù|Û|Ü',
  c: 'ç|Ç',
  n: 'ñ|Ñ'
}
const strEpuree = str => Object.keys(accentsMap).reduce((acc, cur) => acc.replace(new RegExp(accentsMap[cur], 'g'), cur), str.replaceAll('[^a-zA-Z0-9]', ''))

@marcelo-ribeiro
Copy link
Author

marcelo-ribeiro commented Mar 11, 2021

Thank you for sharing. Note that ä,Ä,ë,Ë,ï,Ï,ö,Ö are missing in the map.

🤙🏻

@a2br
Copy link

a2br commented Apr 8, 2021

Maybe you should check this out! :) Remove accents/diacritics in a string in JavaScript

@N0NameF0und
Copy link

N0NameF0und commented May 13, 2021

Love it thank you so much <3

@gtobar
Copy link

gtobar commented May 15, 2021

Excellent work Marcelo, thanks you for sharing.
It didn't replace whitespace well, so I mixed the two lines together and it works like a charm.

``
const slugify = str => {
const map = {
'-' : ' |_',
'a' : 'á|à|ã|â|ä|À|Á|Ã|Â|Ä',
'e' : 'é|è|ê|ë|É|È|Ê|Ë',
'i' : 'í|ì|î|ï|Í|Ì|Î|Ï',
'o' : 'ó|ò|ô|õ|ö|Ó|Ò|Ô|Õ|Ö',
'u' : 'ú|ù|û|ü|Ú|Ù|Û|Ü',
'c' : 'ç|Ç',
'n' : 'ñ|Ñ'
};

for (var pattern in map) {
str = str.replace(new RegExp(map[pattern], 'g'), pattern);
}

return str;
}
``

@jhegner
Copy link

jhegner commented May 16, 2021

Thanks @marcelo,

I found this interesting topic on the stackoverflow using String.prototype.normalize()

Remove Accents in a String

@oasis1992
Copy link

oasis1992 commented Jun 28, 2021

const accentsMap = {
  a: 'á|à|ã|â|À|Á|Ã|Â',
  e: 'é|è|ê|É|È|Ê',
  i: 'í|ì|î|Í|Ì|Î',
  o: 'ó|ò|ô|õ|Ó|Ò|Ô|Õ',
  u: 'ú|ù|û|ü|Ú|Ù|Û|Ü',
  c: 'ç|Ç',
  n: 'ñ|Ñ',
};

export const slugify = text => Object.keys(accentsMap).reduce((acc, cur) => acc.replace(new RegExp(accentsMap[cur], 'g'), cur), text);

thanks! and thanks to the author @marcelo-ribeiro

@marcelo-ribeiro
Copy link
Author

marcelo-ribeiro commented Jul 1, 2021

New ES6 version! 😎

@marcelo-ribeiro
Copy link
Author

marcelo-ribeiro commented Jul 1, 2021

Thanks a lot for this snippet.
I improved for ES6 use, in case anyone might want it:

const accentsMap = {
  a: 'á|à|ã|â|À|Á|Ã|Â',
  e: 'é|è|ê|É|È|Ê',
  i: 'í|ì|î|Í|Ì|Î',
  o: 'ó|ò|ô|õ|Ó|Ò|Ô|Õ',
  u: 'ú|ù|û|ü|Ú|Ù|Û|Ü',
  c: 'ç|Ç',
  n: 'ñ|Ñ',
};

export const slugify = text => Object.keys(accentsMap).reduce((acc, cur) => acc.replace(new RegExp(accentsMap[cur], 'g'), cur), text);

Thank you for sharing! I used part of the your code to improve the ES6 version.
I hope you enjoy 😃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment