Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
JAVASCRIPT:Remove Accents
function RemoveAccents(strAccents) {
var strAccents = strAccents.split('');
var strAccentsOut = new Array();
var strAccentsLen = strAccents.length;
var accents = 'ÀÁÂÃÄÅàáâãäåÒÓÔÕÕÖØòóôõöøÈÉÊËèéêëðÇçÐÌÍÎÏìíîïÙÚÛÜùúûüÑñŠšŸÿýŽž';
var accentsOut = "AAAAAAaaaaaaOOOOOOOooooooEEEEeeeeeCcDIIIIiiiiUUUUuuuuNnSsYyyZz";
for (var y = 0; y < strAccentsLen; y++) {
if (accents.indexOf(strAccents[y]) != -1) {
strAccentsOut[y] = accentsOut.substr(accents.indexOf(strAccents[y]), 1);
} else
strAccentsOut[y] = strAccents[y];
}
strAccentsOut = strAccentsOut.join('');
return strAccentsOut;
}
@espellcaste
Copy link

espellcaste commented Mar 4, 2015

Really helpful! Thanks! =)

@davidgaroro
Copy link

davidgaroro commented Feb 6, 2016

I like it!.
I've modified it a little to less code:

function RemoveAccents(str) {
  var accents    = 'ÀÁÂÃÄÅàáâãäåÒÓÔÕÕÖØòóôõöøÈÉÊËèéêëðÇçÐÌÍÎÏìíîïÙÚÛÜùúûüÑñŠšŸÿýŽž';
  var accentsOut = "AAAAAAaaaaaaOOOOOOOooooooEEEEeeeeeCcDIIIIiiiiUUUUuuuuNnSsYyyZz";
  str = str.split('');
  var strLen = str.length;
  var i, x;
  for (i = 0; i < strLen; i++) {
    if ((x = accents.indexOf(str[i])) != -1) {
      str[i] = accentsOut[x];
    }
  }
  return str.join('');
}

Thank you!

@mrchess
Copy link

mrchess commented Feb 10, 2017

Doesn't cover ß

@b4dnewz
Copy link

b4dnewz commented Oct 2, 2017

with ß:

function RemoveAccents(str) {
  var accents    = 'ÀÁÂÃÄÅàáâãäåßÒÓÔÕÕÖØòóôõöøÈÉÊËèéêëðÇçÐÌÍÎÏìíîïÙÚÛÜùúûüÑñŠšŸÿýŽž';
  var accentsOut = "AAAAAAaaaaaaBOOOOOOOooooooEEEEeeeeeCcDIIIIiiiiUUUUuuuuNnSsYyyZz";
  str = str.split('');
  var strLen = str.length;
  var i, x;
  for (i = 0; i < strLen; i++) {
    if ((x = accents.indexOf(str[i])) != -1) {
      str[i] = accentsOut[x];
    }
  }
  return str.join('');
}

@yamid
Copy link

yamid commented Oct 13, 2017

with ES6:

function removeAccents(str) {
  let accents = 'ÀÁÂÃÄÅàáâãäåßÒÓÔÕÕÖØòóôõöøÈÉÊËèéêëðÇçÐÌÍÎÏìíîïÙÚÛÜùúûüÑñŠšŸÿýŽž';
  let accentsOut = "AAAAAAaaaaaaBOOOOOOOooooooEEEEeeeeeCcDIIIIiiiiUUUUuuuuNnSsYyyZz";
  str = str.split('');
  str.forEach((letter, index) => {
    let i = accents.indexOf(letter);
    if (i != -1) {
      str[index] = accentsOut[i];
    }
  })
  return str.join('');
}

@max1020
Copy link

max1020 commented Nov 7, 2017

if someone needs slovak letters:
ÝŤŔŇĽĹDŽĎČ were missing

	'ÀÁÂÃÄÅàáâãäåßÒÓÔÕÕÖØòóôõöøĎďDŽdžÈÉÊËèéêëðÇçČčÐÌÍÎÏìíîïÙÚÛÜùúûüĽĹľĺÑŇňñŔ੹ŤťŸÝÿýŽž';
	"AAAAAAaaaaaasOOOOOOOooooooDdDZdzEEEEeeeeeCcCcDIIIIiiiiUUUUuuuuLLllNNnnRrSsTtYYyyZz";

@SamirSouzaSys
Copy link

SamirSouzaSys commented Dec 19, 2017

Thanks!!

@ArthurMaroulier
Copy link

ArthurMaroulier commented Dec 29, 2017

What about this ? It's shorter :)
str.normalize('NFD').replace(/[\u0300-\u036f]/g, "")

@jcezarms
Copy link

jcezarms commented Jan 4, 2018

It really is, Arthur, thank you! It's good to also point to the original, mainly for its complete explanation.

@HUN-Julian
Copy link

HUN-Julian commented Jan 18, 2018

...with special hungarian characters:

  let accents = 'ÀÁÂÃÄÅàáâãäåßÒÓÔÕÕÖØŐòóôőõöøĎďDŽdžÈÉÊËèéêëðÇçČčÐÌÍÎÏìíîïÙÚÛÜŰùűúûüĽĹľĺÑŇňñŔ੹ŤťŸÝÿýŽž';
  let accentsOut = "AAAAAAaaaaaasOOOOOOOOoooooooDdDZdzEEEEeeeeeCcCcDIIIIiiiiUUUUUuuuuuLLllNNnnRrSsTtYYyyZz";

Copy link

ghost commented Jan 20, 2018

and with special polish characters:
let accents = 'ÀÁÂÃÄÅĄàáâãäåąßÒÓÔÕÕÖØŐòóôőõöøĎďDŽdžÈÉÊËĘèéêëęðÇçČčĆćÐÌÍÎÏìíîïÙÚÛÜŰùűúûüĽĹŁľĺłÑŇŃňñńŔ੦šśŤťŸÝÿýŽŻŹžżź'; let accentsOut = "AAAAAAAaaaaaaasOOOOOOOOoooooooDdDZdzEEEEEeeeeeeCcCcCcDIIIIiiiiUUUUUuuuuuLLLlllNNNnnnRrSSssTtYYyyZZZzzz";

@FerreiraRaphael
Copy link

FerreiraRaphael commented Feb 16, 2018

with map:

function removeAccents(string) {
      const accents =
        "ÀÁÂÃÄÅàáâãäåßÒÓÔÕÕÖØòóôõöøÈÉÊËèéêëðÇçÐÌÍÎÏìíîïÙÚÛÜùúûüÑñŠšŸÿýŽž";
      const accentsOut =
        "AAAAAAaaaaaaBOOOOOOOooooooEEEEeeeeeCcDIIIIiiiiUUUUuuuuNnSsYyyZz";
      return string
        .split("")
        .map((letter, index) => {
          const accentIndex = accents.indexOf(letter);
          return accentIndex !== -1 ? accentsOut[accentIndex] : letter;
        })
        .join("");
    }

@smietanka
Copy link

smietanka commented Mar 14, 2018

@up with polish special characters

function removeAccents(string) {
      const accents =
        "ÀÁÂÃÄÅĄàáâãäåąßÒÓÔÕÕÖØÓòóôõöøóÈÉÊËĘèéêëęðÇĆçćÐÌÍÎÏìíîïÙÚÛÜùúûüÑŃñńŠŚšśŸÿýŽŻŹžżź";
      const accentsOut =
        "AAAAAAAaaaaaaaBOOOOOOOOoooooooEEEEEeeeeeeCCccDIIIIiiiiUUUUuuuuNNnnSSssYyyZZZzzz";
      return string
        .split("")
        .map((letter, index) => {
          const accentIndex = accents.indexOf(letter);
          return accentIndex !== -1 ? accentsOut[accentIndex] : letter;
        })
        .join("");
    }

@brunoocasali
Copy link

brunoocasali commented May 17, 2018

If you use ES6 consider this Stack answer: https://stackoverflow.com/a/37511463/2649707

@darkylmnx
Copy link

darkylmnx commented Jun 13, 2018

@brunoocasali doesn't work with "œ" char, like in "Cœur"

@juancama
Copy link

juancama commented Aug 20, 2018

Other variant without constants

function normalizeString (string) {
  return string.split('').map(function (letter) {
      let i = this.accents.indexOf(letter)
      return (i !== -1) ? this.out[i] : letter
    }.bind({
      accents: 'ÀÁÂÃÄÅĄàáâãäåąßÒÓÔÕÕÖØÓòóôõöøóÈÉÊËĘèéêëęðÇĆçćÐÌÍÎÏìíîïÙÚÛÜùúûüÑŃñńŠŚšśŸÿýŽŻŹžżź',
      out: 'AAAAAAAaaaaaaaBOOOOOOOOoooooooEEEEEeeeeeeCCccDIIIIiiiiUUUUuuuuNNnnSSssYyyZZZzzz'
    })
  ).join('')
}

@Knud13
Copy link

Knud13 commented Sep 4, 2018

I think the German eszett ß should be an "s" https://en.wikipedia.org/wiki/%C3%9F
β is the greek beta https://en.wikipedia.org/wiki/Beta and that should probably be a "B"

@jpbberry
Copy link

jpbberry commented Sep 30, 2018

Added a few

var accents = 'ÀÁÂÃÄÅĄĀāàáâãäåąßÒÓÔÕÕÖØŐòóôőõöøĎďDŽdžÈÉÊËĘèéêëęðÇçČčĆćÐÌÍÎÏĪìíîïīÙÚÛÜŰùűúûüĽĹŁľĺłÑŇŃňñńŔŕŠŚŞšśşŤťŸÝÿýŽŻŹžżźđĢĞģğ';
var accentsOut = "AAAAAAAAaaaaaaaasOOOOOOOOoooooooDdDZdzEEEEEeeeeeeCcCcCcDIIIIIiiiiiUUUUUuuuuuLLLlllNNNnnnRrSSSsssTtYYyyZZZzzzdGGgg";

@oliversisson
Copy link

oliversisson commented Oct 15, 2018

How about this?

var nIC = new Intl.Collator(undefined, {sensitivity: 'base'})
var cmp = nIC.compare.bind(nIC)

Or this?

'être'.localeCompare('etre', undefined, {sensitivity: 'base'})

@Valegox
Copy link

Valegox commented Dec 12, 2018

Just... thank you !

@hotdang-ca
Copy link

hotdang-ca commented Mar 11, 2019

Though I prefer the map method, here's the original but TypeScript-friendly:

private replaceAccents(str: string): string
{
    const ACCENTS = 'ÀÁÂÃÄÅàáâãäåÒÓÔÕÕÖØòóôõöøÈÉÊËèéêëðÇçÐÌÍÎÏìíîïÙÚÛÜùúûüÑñŠšŸÿýŽž';
    const NON_ACCENTS = "AAAAAAaaaaaaOOOOOOOooooooEEEEeeeeeCcDIIIIiiiiUUUUuuuuNnSsYyyZz";

    const strAccents: string[] = str.split('');
    const strAccentsOut: string[] = new Array();

    const strAccentsLen: number = strAccents.length;

    for (let y = 0; y < strAccentsLen; y++) 
    {
        if (ACCENTS.indexOf(strAccents[y]) != -1)
        {
            strAccentsOut[y] = NON_ACCENTS.substr(ACCENTS.indexOf(strAccents[y]), 1);
        } 
        else
        {
                strAccentsOut[y] = strAccents[y];
        }
    }

    const newString: string = strAccentsOut.join('');
    return newString;
}

@tomjohnburton
Copy link

tomjohnburton commented Nov 6, 2019

A slight alternative to above using the string normalisation AND correct substitution for the German ß

import { includes, some } from 'lodash'

export default function convertForeignCharacters(str: string): string {
  const strAccents: string[] = str.split('')
  const strAccentsOut: string[] = new Array()

  const strAccentsLen: number = strAccents.length

  for (let y: number = 0; y < strAccentsLen; y++) {
    if (!some(['ä', 'ü', 'ö'], (el: string) => includes(strAccents[y], el))) {
      strAccentsOut[y] = strAccents[y].normalize('NFD').replace(/[\u0300-\u036f]/g, '')
    } else {
      strAccentsOut[y] = strAccents[y]
    }
  }

  const newString: string = strAccentsOut.join('').replace('ß', 'ss')
  return newString
}

@lsarrazi
Copy link

lsarrazi commented Aug 6, 2020

O(n) version, ~5x faster, especially if you want add more accents.

const accents = 'ÀÁÂÃÄÅĄĀāàáâãäåąßÒÓÔÕÕÖØŐòóôőõöøĎďDŽdžÈÉÊËĘèéêëęðÇçČčĆćÐÌÍÎÏĪìíîïīÙÚÛÜŰùűúûüĽĹŁľĺłÑŇŃňñńŔŕŠŚŞšśşŤťŸÝÿýŽŻŹžżźđĢĞģğ',
	accents_out = "AAAAAAAAaaaaaaaasOOOOOOOOoooooooDdDZdzEEEEEeeeeeeCcCcCcDIIIIIiiiiiUUUUUuuuuuLLLlllNNNnnnRrSSSsssTtYYyyZZZzzzdGGgg",
	accents_map = new Map();
for (const i in accents)
	accents_map.set(accents.charCodeAt(i), accents_out.charCodeAt(i))

function removeAccents(str) {
	const nstr = new Array(str.length);
	let x, i;
	for (i = 0; i < nstr.length; i++)
		nstr[i] = accents_map.get(x = str.charCodeAt(i)) || x;
	return String.fromCharCode.apply(null, nstr);
}

@CramericaIndustries
Copy link

CramericaIndustries commented Oct 9, 2020

@ArthurMaroulier
FYI: It doesn't cover all letters ('Ø' for example).

"ÀÁÂÃÄÅàáâãäåÒÓÔÕÕÖØòóôõöøÈÉÊËèéêëðÇçÐÌÍÎÏìíîïÙÚÛÜùúûüÑñŠšŸÿýŽž".normalize('NFD').replace(/[\u0300-\u036f]/g, "");
"AAAAAAaaaaaaOOOOOOØoooooøEEEEeeeeðCcÐIIIIiiiiUUUUuuuuNnSsYyyZz"

@jrking365
Copy link

jrking365 commented Apr 15, 2021

did you find a way to convert Ø please ?

@yosietserga
Copy link

yosietserga commented May 28, 2021

@alisterlf
Copy link
Author

alisterlf commented Aug 22, 2021

If your goal is to sort a list of strings, disregarding accents

['André', 'Álister', 'alan'].sort(new Intl.Collator('pt-BR').compare)

Or just to be safe

['André', 'Álister', 'alan'].sort(new Intl.Collator('pt-BR', {sensitivity:'base'}).compare)

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