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

vegas world casino games casino game zone online casino games | https://onlinecasino2018.us.org/ - free vegas casino games

doubledown casino online casinos las vegas casinos | https://onlinecasino2018.us.org/ - casino slots

doubledown casino online casinos las vegas casinos | https://onlinecasino2018.us.org/ - casino slots

virgin online casino borgata online casino free casino games vegas world | https://onlinecasino2018.us.org/ - casino games slots free

foxwoods online casino doubledown casino free casino games slots | https://onlinecasino2018.us.org/ - big fish casino

foxwoods online casino doubledown casino free casino games slots | https://onlinecasino2018.us.org/ - big fish casino

winstar world casino free vegas casino games free casino games sun moon | https://onlinecasino2018.us.org/ - online gambling casino

online casino games parx online casino casino games | https://onlinecasino2018.us.org/ - casino game

online casino games parx online casino casino games | https://onlinecasino2018.us.org/ - casino game

payday advance online payday loan payday loan bad credit loans guaranteed approval online payday cash loans

small payday loans online payday online loan payday loans direct lender paydays loans online

free slots casino games free online slots free online casino games | https://onlinecasino2018.us.org/ - doubledown casino

free slots casino games free online slots free online casino games | https://onlinecasino2018.us.org/ - doubledown casino

chumba casino casino games free online casino games online | https://onlinecasino2018.us.org/ - online casino slots

parx online casino casino blackjack empire city online casino | https://onlinecasino2018.us.org/ - online casino gambling

free casino games no download online casinos casino games | https://onlinecasino2018.us.org/ - online casino games

free casino games no download online casinos casino games | https://onlinecasino2018.us.org/ - online casino games

free casino slot games las vegas casinos casino blackjack | https://onlinecasino2018.us.org/ - casino games free online

free casino slot games las vegas casinos casino blackjack | https://onlinecasino2018.us.org/ - casino games free online

casino bonus codes casino play online casinos for us players | https://onlinecasino2018.us.org/ - play free vegas casino games

casino bonus codes casino play online casinos for us players | https://onlinecasino2018.us.org/ - play free vegas casino games

best online casinos slots for real money mgm online casino | https://onlinecasino2018.us.org/ - lady luck online casino

best online casinos slots for real money mgm online casino | https://onlinecasino2018.us.org/ - lady luck online casino

winstar world casino best online casinos free slots games | https://onlinecasino2018.us.org/ - casino blackjack

winstar world casino best online casinos free slots games | https://onlinecasino2018.us.org/ - casino blackjack

hypercasinos online casinos free casino slot games gsn casino games | https://onlinecasinoplay.us.org/ - tropicana online casino https://onlinecasinoplay.us.org/ - online casinos for us players

bovada casino play online casino online gambling zone online casino | https://onlinecasinoplay.us.org/ - free online casino games https://onlinecasinoplay.us.org/ - online gambling casino

bovada casino play online casino online gambling zone online casino | https://onlinecasinoplay.us.org/ - free online casino games https://onlinecasinoplay.us.org/ - online gambling casino

casino bonus gsn casino games tropicana online casino caesars online casino | https://onlinecasinoplay.us.org/ - online casino gambling https://onlinecasinoplay.us.org/ - online casinos

hollywood casino casino slots tropicana online casino play free vegas casino games | https://onlinecasinoplay.us.org/ - caesars online casino https://onlinecasinoplay.us.org/ - big fish casino

free casino games sun moon high 5 casino firekeepers casino zone online casino games | https://onlinecasinoplay.us.org/ - winstar world casino https://onlinecasinoplay.us.org/ - borgata online casino

free casino games sun moon high 5 casino firekeepers casino zone online casino games | https://onlinecasinoplay.us.org/ - winstar world casino https://onlinecasinoplay.us.org/ - borgata online casino

winstar world casino free casino games no download free casino slot games online casino slots | https://onlinecasinoplay.us.org/ - casino play https://onlinecasinoplay.us.org/ - casino play

virgin online casino real money casino online casino slots play casino | https://onlinecasinoplay.us.org/ - casino online https://onlinecasinoplay.us.org/ - casino games slots free

virgin online casino real money casino online casino slots play casino | https://onlinecasinoplay.us.org/ - casino online https://onlinecasinoplay.us.org/ - casino games slots free

free casino games sun moon lady luck play free vegas casino games high 5 casino | https://onlinecasinoplay.us.org/ - online casino real money https://onlinecasinoplay.us.org/ - borgata online casino

free casino games sun moon lady luck play free vegas casino games high 5 casino | https://onlinecasinoplay.us.org/ - online casino real money https://onlinecasinoplay.us.org/ - borgata online casino

big fish casino chumba casino online casino games mgm online casino | https://onlinecasinoplay.us.org/ - free casino slot games https://onlinecasinoplay.us.org/ - free casino games slots

big fish casino chumba casino online casino games mgm online casino | https://onlinecasinoplay.us.org/ - free casino slot games https://onlinecasinoplay.us.org/ - free casino games slots

borgata online casino casino games casino games slots free las vegas casinos | https://onlinecasinoplay.us.org/ - parx online casino https://onlinecasinoplay.us.org/ - parx online casino

online casino games best online casinos gsn casino slots borgata online casino | https://onlinecasinoplay.us.org/ - hyper casinos https://onlinecasinoplay.us.org/ - casino play

online casino games best online casinos gsn casino slots borgata online casino | https://onlinecasinoplay.us.org/ - hyper casinos https://onlinecasinoplay.us.org/ - casino play

free online slots casino online casino games free online | https://onlinecasino888.us.org/ - best online casino

new viagra cost of generic viagra best price viagra cheapest generic viagra

new viagra cost of generic viagra best price viagra cheapest generic viagra

meadows casino real money casino casino bonus codes vegas casino games | https://onlinecasinox.icu/ - winstar world casino https://onlinecasinox.icu/ - las vegas casinos

double down casino casino games casino bonus river spirit casino tulsa | https://freeonlinecasino.icu/ - free casino games online https://freeonlinecasino.icu/ - gambling sites

hollywood casino online slots free slots games online free slot games online slots free games | https://realmoneyslots.icu/ - free slot games https://realmoneyslots.icu/ - real casino slots

Pages

Add new comment