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

free casino games slots hollywood casino online slots casino games free high 5 casino | https://onlinecasinoo.icu/ - parx casino online https://onlinecasinoo.icu/ - jackpot party casino

free casino games slots hollywood casino online slots casino games free high 5 casino | https://onlinecasinoo.icu/ - parx casino online https://onlinecasinoo.icu/ - jackpot party casino

morongo casino casino games free online virgin casino online nj jackpot party casino facebook | https://playonlinecasino.icu/ - chumba casino https://playonlinecasino.icu/ - ilani casino

doubledown casino ignition casino play online casino parx casino | https://realmoneycasino.ooo/ - casino slot https://realmoneycasino.ooo/ - gsn casino on facebook

cafe casino online free online casino games pala casino online san manuel casino | https://playcasinoslots.ooo/ - caesar casino online slot games https://playcasinoslots.ooo/ - pala casino online

cafe casino online free online casino games pala casino online san manuel casino | https://playcasinoslots.ooo/ - caesar casino online slot games https://playcasinoslots.ooo/ - pala casino online

viagra dosage viagra online prescription free side effects of viagra is there generic viagra

viagra dosage viagra online prescription free side effects of viagra is there generic viagra

free vegas casino games tropicana online casino free casino games vegas world foxwoods online casino | https://onlinecasinoplay.us.org/ - mgm online casino https://onlinecasinoplay.us.org/ - gsn casino slots

free vegas casino games tropicana online casino free casino games vegas world foxwoods online casino | https://onlinecasinoplay.us.org/ - mgm online casino https://onlinecasinoplay.us.org/ - gsn casino slots

best online slots slots free games vegas world slots caesars free slots online | https://slotsrealmoney.icu/ - slots online https://slotsrealmoney.icu/ - slots for free

best online slots slots free games vegas world slots caesars free slots online | https://slotsrealmoney.icu/ - slots online https://slotsrealmoney.icu/ - slots for free

las vegas free slots caesars free slots online penny slots buffalo slots | https://slotgames.icu/ - slots online https://slotgames.icu/ - online slots free

las vegas free slots caesars free slots online penny slots buffalo slots | https://slotgames.icu/ - slots online https://slotgames.icu/ - online slots free

casino games free casino games free doubledown casino promo codes virgin online casino | https://playonlinecasino.icu/ - stn play online casino https://playonlinecasino.icu/ - winstar casino

casino games free online double u casino casinos near me sugarhouse casino | https://onlinecasinoo.icu/ - free slots casino games https://onlinecasinoo.icu/ - zone online casino

casino games free online double u casino casinos near me sugarhouse casino | https://onlinecasinoo.icu/ - free slots casino games https://onlinecasinoo.icu/ - zone online casino

free slots online no download no registration celebrity slots infinity slots slots of vegas | https://slotsrealmoney.icu/ - konami free slots https://slotsrealmoney.icu/ - heart of vegas slots

free slots online no download no registration celebrity slots infinity slots slots of vegas | https://slotsrealmoney.icu/ - konami free slots https://slotsrealmoney.icu/ - heart of vegas slots

slot games free free slot machine games vegas free slots free online slots vegas world | https://slotgames.icu/ - online casino slots no download https://slotgames.icu/ - quick hits free slots

slot games free free slot machine games vegas free slots free online slots vegas world | https://slotgames.icu/ - online casino slots no download https://slotgames.icu/ - quick hits free slots

seneca niagara casino online casinos online casino bonus red rock casino | https://realmoneycasino.ooo/ - play casino https://realmoneycasino.ooo/ - double down casino

seneca niagara casino online casinos online casino bonus red rock casino | https://realmoneycasino.ooo/ - play casino https://realmoneycasino.ooo/ - double down casino

chumash casino bigfish casino online games casino online casino games free online | https://playcasinoslots.ooo/ - double down casino https://playcasinoslots.ooo/ - bovada blackjack

chumash casino bigfish casino online games casino online casino games free online | https://playcasinoslots.ooo/ - double down casino https://playcasinoslots.ooo/ - bovada blackjack

casino bonus online casino slots no deposit casino bovada casino | https://onlinecasinoplay.us.org/ - casino games free https://onlinecasinoplay.us.org/ - free casino

posh casino online lady luck free slots games hypercasinos | https://playonlinecasino.icu/ - sugarhouse casino https://playonlinecasino.icu/ - stn play online casino

posh casino online lady luck free slots games hypercasinos | https://playonlinecasino.icu/ - sugarhouse casino https://playonlinecasino.icu/ - stn play online casino

san manuel online casino free online casino slots online casino hard rock casino tampa | https://onlinecasinoo.icu/ - riverwind casino https://onlinecasinoo.icu/ - casino blackjack

san manuel online casino free online casino slots online casino hard rock casino tampa | https://onlinecasinoo.icu/ - riverwind casino https://onlinecasinoo.icu/ - casino blackjack

viagra generic buying viagra online buy viagra online viagra online usa

viagra generic buying viagra online buy viagra online viagra online usa

golden nugget online casino riverwind casino usa online casino casino blackjack | https://realmoneycasino.ooo/ - bigfish casino online games https://realmoneycasino.ooo/ - empire city casino

golden nugget online casino riverwind casino usa online casino casino blackjack | https://realmoneycasino.ooo/ - bigfish casino online games https://realmoneycasino.ooo/ - empire city casino

world class casino slots free slot games free casino slots games online slot games | https://slotsrealmoney.icu/ - liberty slots casino https://slotsrealmoney.icu/ - free video slots

chinook winds casino chinook winds casino casino real money chumba casino | https://playcasinoslots.ooo/ - hollywood casino play4fun https://playcasinoslots.ooo/ - snoqualmie casino

slots for free buffalo slots hollywood slots free slots online no download | https://slotgames.icu/ - casino slots free https://slotgames.icu/ - slot games free

slots for free buffalo slots hollywood slots free slots online no download | https://slotgames.icu/ - casino slots free https://slotgames.icu/ - slot games free

real casino playmgm nj casino online gsn casino on facebook vegas casino games | https://onlinecasinoo.icu/ - seneca niagara casino https://onlinecasinoo.icu/ - codeshareonline doubledown casino

real casino playmgm nj casino online gsn casino on facebook vegas casino games | https://onlinecasinoo.icu/ - seneca niagara casino https://onlinecasinoo.icu/ - codeshareonline doubledown casino

play online casino free slots games river spirit casino tulsa usa online casino | https://playonlinecasino.icu/ - caesars casino online https://playonlinecasino.icu/ - lady luck online casino

play online casino free slots games river spirit casino tulsa usa online casino | https://playonlinecasino.icu/ - caesars casino online https://playonlinecasino.icu/ - lady luck online casino

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

las vegas casinos sugarhouse casino online casino zone online casino games | https://playcasinoslots.ooo/ - soaring eagle casino https://playcasinoslots.ooo/ - doubledown casino facebook

las vegas casinos sugarhouse casino online casino zone online casino games | https://playcasinoslots.ooo/ - soaring eagle casino https://playcasinoslots.ooo/ - doubledown casino facebook

empire casino online rivers casino online gambling mgm online casino | https://realmoneycasino.ooo/ - choctaw casino durant oklahoma https://realmoneycasino.ooo/ - spirit mountain casino

empire casino online rivers casino online gambling mgm online casino | https://realmoneycasino.ooo/ - choctaw casino durant oklahoma https://realmoneycasino.ooo/ - spirit mountain casino

vegas world free games online slots free slots online vegas slots online slots free games | https://slotgames.icu/ - simslots free slots https://slotgames.icu/ - free online slots games

vegas world free games online slots free slots online vegas slots online slots free games | https://slotgames.icu/ - simslots free slots https://slotgames.icu/ - free online slots games

Pages

Add new comment