Converting text to basic latin (aka removing accents) with JavaScript

I was recently working on a decision engine for a quiz site (KwizMi.com) . The quizzes allow you to define a table of questions and answers, and users playing the quizzes have to try and guess all the answers in a given period of time.

The problem I stumbled upon is best illustrated by the following example:

  • Question: Which "GP" plays at centre-back for Barcelona?
  • Answer: Gerard [Piqué]

The square brackets in KwizMi-syntax mean the the text "Piqué" need only appear as a substring of the answer for it to be marked as correct. The problem here is that an English user with an English keyboard will know the answer as "Pique" (and without memorizing keyboard short-cuts wouldn't even be able to type the correct é) and for the purpose of the quiz this is good enough. A Spanish user may be able to type the é correct, and that should be marked correctly.

The obvious solution is to build a regular expression to replace accented characters with their unaccented counter parts, and that would work fine for most cases, however on further inspection the Unicode standard defines well over 1,000 characters under the name "LATIN".

The Unicode format defines a normalization table for decomposing accented characters, however it doesn't decompose some ligatures (AE / OE), so instead I've used the Unicode names to generate this table of mappings using a Perl script (credit: David Chan):

var latin_map = {
  'Á': 'A', // LATIN CAPITAL LETTER A WITH ACUTE
  'Ă': 'A', // LATIN CAPITAL LETTER A WITH BREVE
...
  'ᵥ': 'v', // LATIN SUBSCRIPT SMALL LETTER V
  'ₓ': 'x', // LATIN SUBSCRIPT SMALL LETTER X
};

Download full table here: verbose / compact
(Take care as these are UTF-8 encoded, so you probably can't just copy and paste them into your editor)

And the following extensions to the String object:

String.prototype.latinise = function() {
	return this.replace(/[^A-Za-z0-9]/g, function(x) { return latin_map[x] || x; })
};
 
// American English spelling :)
String.prototype.latinize = String.prototype.latinise;
 
String.prototype.isLatin = function() {
	return this == this.latinise();
};

Here are some examples:

> "Piqué".latinise();
"Pique"
> "Piqué".isLatin();
false
> "Pique".isLatin();
true
> "Piqué".latinise().isLatin();
true

All scripts are Public Domain.

AttachmentSize
Plain text icon latinise.js (Source)54.18 KB
Plain text icon latinise.min.js (Minified)7.98 KB
Plain text icon latin_map.pl37.01 KB

Comments

pala casino online online casino real money online casino posh casino | https://onlinecasinoo.icu/ - foxwoods casino online https://onlinecasinoo.icu/ - hollywood casino

double u casino pala casino online nj hypercasinos chumba casino | https://playonlinecasino.icu/ - tropicana online casino https://playonlinecasino.icu/ - parx online casino

double u casino pala casino online nj hypercasinos chumba casino | https://playonlinecasino.icu/ - tropicana online casino https://playonlinecasino.icu/ - parx online casino

lady luck online casino sugarhouse casino hypercasinos borgata hotel casino | https://realmoneycasino.ooo/ - winstar casino oklahoma https://realmoneycasino.ooo/ - thunder valley casino

caesar casino online slot games hard rock casino atlantic city doubledown casino downstream casino | https://playcasinoslots.ooo/ - paradise casino https://playcasinoslots.ooo/ - mountaineer casino

caesar casino online slot games hard rock casino atlantic city doubledown casino downstream casino | https://playcasinoslots.ooo/ - paradise casino https://playcasinoslots.ooo/ - mountaineer casino

slots for free house of fun free slots free slots casino games casino games slots free | https://slotsrealmoney.icu/ - slots free https://slotsrealmoney.icu/ - cleopatra slots

free penny slots gamepoint slots goldfish slots slots online | https://slotgames.icu/ - free games online slots https://slotgames.icu/ - free slots online no download no registration

free penny slots gamepoint slots goldfish slots slots online | https://slotgames.icu/ - free games online slots https://slotgames.icu/ - free slots online no download no registration

caesars online casino best online casinos casino games free vegas world casino games | https://onlinecasinoplay.us.org/ - free casino games https://onlinecasinoplay.us.org/ - free casino games slots

caesars online casino best online casinos casino games free vegas world casino games | https://onlinecasinoplay.us.org/ - free casino games https://onlinecasinoplay.us.org/ - free casino games slots

double down casino soaring eagle casino harrah cherokee casino hallmark casino online | https://onlinecasinoo.icu/ - ilani casino https://onlinecasinoo.icu/ - doubledown casino facebook

double down casino soaring eagle casino harrah cherokee casino hallmark casino online | https://onlinecasinoo.icu/ - ilani casino https://onlinecasinoo.icu/ - doubledown casino facebook

riverwind casino pechanga casino free casino games vegas world downstream casino | https://playonlinecasino.icu/ - san manuel online casino https://playonlinecasino.icu/ - free casino games slot machines

riverwind casino pechanga casino free casino games vegas world downstream casino | https://playonlinecasino.icu/ - san manuel online casino https://playonlinecasino.icu/ - free casino games slot machines

pop slots hollywood slots mohegan sun free online slots free vegas slots online | https://slotgames.icu/ - ruby slots https://slotgames.icu/ - liberty slots

pop slots hollywood slots mohegan sun free online slots free vegas slots online | https://slotgames.icu/ - ruby slots https://slotgames.icu/ - liberty slots

ilani casino virgin casino online real casino slots penny slots | https://playcasinoslots.ooo/ - casinos near me https://playcasinoslots.ooo/ - foxwoods casino online slots

ilani casino virgin casino online real casino slots penny slots | https://playcasinoslots.ooo/ - casinos near me https://playcasinoslots.ooo/ - foxwoods casino online slots

free slots vegas world casino slots free games mirrorball slots vegas slots online | https://slotsrealmoney.icu/ - slots lounge https://slotsrealmoney.icu/ - slots free online

free slots vegas world casino slots free games mirrorball slots vegas slots online | https://slotsrealmoney.icu/ - slots lounge https://slotsrealmoney.icu/ - slots free online

online casino slots pechanga casino huuuge casino free casino games | https://realmoneycasino.ooo/ - pechanga casino https://realmoneycasino.ooo/ - san manuel casino

online casino slots pechanga casino huuuge casino free casino games | https://realmoneycasino.ooo/ - pechanga casino https://realmoneycasino.ooo/ - san manuel casino

pala casino online casino games free pala casino online nj greektown casino | https://onlinecasinoo.icu/ - online gambling casino https://onlinecasinoo.icu/ - parx casino online

pala casino online casino games free pala casino online nj greektown casino | https://onlinecasinoo.icu/ - online gambling casino https://onlinecasinoo.icu/ - parx casino online

doubledown casino play online casino casino real money lady luck | https://onlinecasinoplay.us.org/ - casino game https://onlinecasinoplay.us.org/ - winstar world casino

doubledown casino play online casino casino real money lady luck | https://onlinecasinoplay.us.org/ - casino game https://onlinecasinoplay.us.org/ - winstar world casino

free casino games slotomania oxford casino penny slots vegas casino online | https://realmoneycasino.ooo/ - caesars casino https://realmoneycasino.ooo/ - casino slots

free casino games slotomania oxford casino penny slots vegas casino online | https://realmoneycasino.ooo/ - caesars casino https://realmoneycasino.ooo/ - casino slots

hampton beach casino ballroom downstream casino casino play grand falls casino | https://playcasinoslots.ooo/ - harrah cherokee casino https://playcasinoslots.ooo/ - casino games

hampton beach casino ballroom downstream casino casino play grand falls casino | https://playcasinoslots.ooo/ - harrah cherokee casino https://playcasinoslots.ooo/ - casino games

golden nugget online casino double u casino empire city online casino jack online casino | https://onlinecasinoo.icu/ - thunder valley casino https://onlinecasinoo.icu/ - play online casino

golden nugget online casino double u casino empire city online casino jack online casino | https://onlinecasinoo.icu/ - thunder valley casino https://onlinecasinoo.icu/ - play online casino

sugarhouse casino online blue chip casino zone online casino games gun lake casino | https://playonlinecasino.icu/ - twin river casino https://playonlinecasino.icu/ - codeshareonline doubledown casino

sugarhouse casino online blue chip casino zone online casino games gun lake casino | https://playonlinecasino.icu/ - twin river casino https://playonlinecasino.icu/ - codeshareonline doubledown casino

free casino games slot machines doubledown casino facebook parx online casino hollywood casino online | https://playcasinoslots.ooo/ - slots for real money https://playcasinoslots.ooo/ - ilani casino

free casino games slot machines doubledown casino facebook parx online casino hollywood casino online | https://playcasinoslots.ooo/ - slots for real money https://playcasinoslots.ooo/ - ilani casino

free buffalo slots monopoly slots free penny slots no download pch slots | https://slotgames.icu/ - casino games slots free https://slotgames.icu/ - free slots vegas world

free buffalo slots monopoly slots free penny slots no download pch slots | https://slotgames.icu/ - casino games slots free https://slotgames.icu/ - free slots vegas world

online casino bonus free casino games sun moon casino real money online casino bonus | https://onlinecasinoplay.us.org/ - las vegas casinos https://onlinecasinoplay.us.org/ - casino blackjack

online casino bonus free casino games sun moon casino real money online casino bonus | https://onlinecasinoplay.us.org/ - las vegas casinos https://onlinecasinoplay.us.org/ - casino blackjack

sugarhouse casino online hallmark casino online online casino pala casino online | https://playcasinoslots.ooo/ - gun lake casino https://playcasinoslots.ooo/ - hampton beach casino ballroom

quick hit slots slots free online scatter slots slot games | https://slotgames.icu/ - konami free slots https://slotgames.icu/ - free slot games with no download

quick hit slots slots free online scatter slots slot games | https://slotgames.icu/ - konami free slots https://slotgames.icu/ - free slot games with no download

slotomania on facebook myvegas slots casino games slots free free online slots no download no registration | https://slotsrealmoney.icu/ - free slots vegas https://slotsrealmoney.icu/ - free slots online no download no registration

slotomania on facebook myvegas slots casino games slots free free online slots no download no registration | https://slotsrealmoney.icu/ - free slots vegas https://slotsrealmoney.icu/ - free slots online no download no registration

foxwoods online casino login free online slots online gambling golden nugget online casino | [url=https://onlinecasinoo.icu/]choctaw casino[/url] [url=https://onlinecasinoo.icu/]casino games[/url]

foxwoods online casino login free online slots online gambling golden nugget online casino | [url=https://onlinecasinoo.icu/]choctaw casino[/url] [url=https://onlinecasinoo.icu/]casino games[/url]

sandia casino turning stone online casino vegas casino online play casino | https://realmoneycasino.ooo/ - casino games https://realmoneycasino.ooo/ - jackpot party casino facebook

sandia casino turning stone online casino vegas casino online play casino | https://realmoneycasino.ooo/ - casino games https://realmoneycasino.ooo/ - jackpot party casino facebook

Pages

Add new comment