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 free slots slot games free free slot games with no download online slots | https://vegasslotsonline.icu/ - free slots with no download or registration https://vegasslotsonline.icu/ - hollywood casino online slots free

gsn casino games casino games free online betfair casino online nj free casino games online | https://freeonlinecasino.icu/ - foxwoods online casino login https://freeonlinecasino.icu/ - best online casino

gsn casino games casino games free online betfair casino online nj free casino games online | https://freeonlinecasino.icu/ - foxwoods online casino login https://freeonlinecasino.icu/ - best online casino

free online casino rivers casino pittsburgh caesars casino online foxwoods casino online | https://slotsonline.ooo/ - 888 casino https://slotsonline.ooo/ - cherokee casino

borgata online casino grand casino hinckley casino online penny slots | https://casinoo.ooo/ - vegas casino games https://casinoo.ooo/ - doubleu casino on facebook

gold fish casino slots vegas slots free casino slots igt free slots | https://realmoneyslots.icu/ - online casino slots no download https://realmoneyslots.icu/ - slots free

gold fish casino slots vegas slots free casino slots igt free slots | https://realmoneyslots.icu/ - online casino slots no download https://realmoneyslots.icu/ - slots free

free slot machine games vegas slots online goldfish casino slots free pch slots tournament | https://vegasslotsonline.icu/ - vegas slots https://vegasslotsonline.icu/ - slots of vegas casino

double u casino vegas world casino games foxwoods casino online slots spirit mountain casino | https://onlinecasinox.icu/ - sugarhouse online casino https://onlinecasinox.icu/ - online casino games free

three rivers casino thunder valley casino potawatomi casino belterra casino | https://freeonlinecasino.icu/ - doubledown casino promo codes https://freeonlinecasino.icu/ - hampton beach casino ballroom

hollywood casino free online casino games big fish casino free casino games | https://slotsonline.ooo/ - no deposit casino https://slotsonline.ooo/ - free casino games vegas world

viejas casino online casinos plainridge casino free online slots | https://casinoo.ooo/ - ignition casino https://casinoo.ooo/ - maryland live casino online

free online slots no download no registration brian christopher slots old vegas slots play slots online | https://vegasslotsonline.icu/ - old vegas slots https://vegasslotsonline.icu/ - slot machine games free

free online slots no download no registration brian christopher slots old vegas slots play slots online | https://vegasslotsonline.icu/ - old vegas slots https://vegasslotsonline.icu/ - slot machine games free

posh casino hollywood casino online casinos online casino | https://freeonlinecasino.icu/ - casino games online https://freeonlinecasino.icu/ - potawatomi casino

gsn casino games hard rock casino atlantic city grand casino hinckley free casino games sun moon | https://casinoo.ooo/ - riverwind casino https://casinoo.ooo/ - free casino games online

gsn casino games hard rock casino atlantic city grand casino hinckley free casino games sun moon | https://casinoo.ooo/ - riverwind casino https://casinoo.ooo/ - free casino games online

san manuel online casino borgata online casino hollywood casino play4fun free casino games slotomania | https://slotsonline.ooo/ - dakota sioux casino https://slotsonline.ooo/ - hard rock casino tampa

san manuel online casino borgata online casino hollywood casino play4fun free casino games slotomania | https://slotsonline.ooo/ - dakota sioux casino https://slotsonline.ooo/ - hard rock casino tampa

online slot games hearts of vegas free slots slot machines gamepoint slots | https://vegasslotsonline.icu/ - vegas slots online free https://vegasslotsonline.icu/ - slots free games

online slot games hearts of vegas free slots slot machines gamepoint slots | https://vegasslotsonline.icu/ - vegas slots online free https://vegasslotsonline.icu/ - slots free games

pompeii slots hollywood casino online slots scatter slots pompeii slots | https://realmoneyslots.icu/ - penny slots free https://realmoneyslots.icu/ - vegas world free slots

pompeii slots hollywood casino online slots scatter slots pompeii slots | https://realmoneyslots.icu/ - penny slots free https://realmoneyslots.icu/ - vegas world free slots

ignition casino free online casino slots jackpot party casino winstar world casino | https://casinoo.ooo/ - big fish casino slots https://casinoo.ooo/ - cafe casino online

ignition casino free online casino slots jackpot party casino winstar world casino | https://casinoo.ooo/ - big fish casino slots https://casinoo.ooo/ - cafe casino online

chinook winds casino slots casino games grand casino hinckley free slots games | https://onlinecasinox.icu/ - free casino games no download https://onlinecasinox.icu/ - ignition casino

chinook winds casino slots casino games grand casino hinckley free slots games | https://onlinecasinox.icu/ - free casino games no download https://onlinecasinox.icu/ - ignition casino

free casino games slot machines hollywood casino online slots free vegas slots online casino winstar casino oklahoma | https://freeonlinecasino.icu/ - best online casinos https://freeonlinecasino.icu/ - sandia casino

free casino games slot machines hollywood casino online slots free vegas slots online casino winstar casino oklahoma | https://freeonlinecasino.icu/ - best online casinos https://freeonlinecasino.icu/ - sandia casino

choctaw casino doubleu casino parx casino online casino bonus | https://slotsonline.ooo/ - borgata online casino https://slotsonline.ooo/ - empire casino online

choctaw casino doubleu casino parx casino online casino bonus | https://slotsonline.ooo/ - borgata online casino https://slotsonline.ooo/ - empire casino online

discount viagra buy real viagra online buy cheap generic viagra when does viagra go generic

free slots games online jackpot party casino slots online casino slots pop slots casino | https://slotsrealmoney.icu/ - vegas free slots online https://slotsrealmoney.icu/ - play slots online

thunder valley casino rivers casino schenectady slot machines gsn casino games | https://realmoneycasino.ooo/ - free casino games online https://realmoneycasino.ooo/ - best online casinos

thunder valley casino rivers casino schenectady slot machines gsn casino games | https://realmoneycasino.ooo/ - free casino games online https://realmoneycasino.ooo/ - best online casinos

free slots 777 house of fun slots free penny slots no download slots farm | https://slotgames.icu/ - vegas slots https://slotgames.icu/ - free slot play no download

free slots 777 house of fun slots free penny slots no download slots farm | https://slotgames.icu/ - vegas slots https://slotgames.icu/ - free slot play no download

huuuge casino red rock casino dakota sioux casino plainridge casino | https://playonlinecasino.icu/ - empire casino online https://playonlinecasino.icu/ - real casino slots

huuuge casino red rock casino dakota sioux casino plainridge casino | https://playonlinecasino.icu/ - empire casino online https://playonlinecasino.icu/ - real casino slots

fortune bay casino blue chip casino seneca allegany casino online casino bonus | https://onlinecasinoo.icu/ - greektown casino https://onlinecasinoo.icu/ - jackpot party casino

fortune bay casino blue chip casino seneca allegany casino online casino bonus | https://onlinecasinoo.icu/ - greektown casino https://onlinecasinoo.icu/ - jackpot party casino

parx online casino free casino doubledown casino doubledown casino | https://playcasinoslots.ooo/ - casino slots https://playcasinoslots.ooo/ - hollywood online casino

parx online casino free casino doubledown casino doubledown casino | https://playcasinoslots.ooo/ - casino slots https://playcasinoslots.ooo/ - hollywood online casino

hyper casinos tropicana online casino gsn casino san manuel online casino | https://realmoneycasino.ooo/ - play online casino https://realmoneycasino.ooo/ - clearwater casino

free slots vegas wizard of oz slots slots free online play slots | https://slotsrealmoney.icu/ - slots free https://slotsrealmoney.icu/ - myvegas slots

casino games slots free free casino slots free vegas slots cashman casino slots free | https://slotgames.icu/ - free slots games online https://slotgames.icu/ - free slots games online

river spirit casino tulsa big fish casino double down casino casino slot | https://playcasinoslots.ooo/ - grand falls casino https://playcasinoslots.ooo/ - free casino games slots

real money casino free casino games vegas world lady luck online casino hypercasinos | https://onlinecasinoplay.us.org/ - online casinos https://onlinecasinoplay.us.org/ - parx online casino

play online casino posh casino online caesars casino casino games | https://playonlinecasino.icu/ - free casino games sun moon https://playonlinecasino.icu/ - free casino games no download

ilani casino red rock casino online casino slots sugarhouse casino | https://onlinecasinoo.icu/ - mgm online casino nj https://onlinecasinoo.icu/ - high five casino slots

Pages

Add new comment