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

viagra patent cheapest generic viagra purchase viagra online generic viagra usa

viagra patent cheapest generic viagra purchase viagra online generic viagra usa

free online casino free casino games slotomania foxwoods online casino no deposit casino | https://onlinecasinoplay.us.org/ - online casinos for us players https://onlinecasinoplay.us.org/ - free slots games

free online casino free casino games slotomania foxwoods online casino no deposit casino | https://onlinecasinoplay.us.org/ - online casinos for us players https://onlinecasinoplay.us.org/ - free slots games

free slots games cafe casino online muckleshoot casino rivers casino | https://playcasinoslots.ooo/ - high five casino slots https://playcasinoslots.ooo/ - prairie meadows casino

caesars slots free casino gsn casino slots play free slots for fun freeslots.com slots | https://slotgames.icu/ - free slots https://slotgames.icu/ - slots of vegas casino

caesars slots free casino gsn casino slots play free slots for fun freeslots.com slots | https://slotgames.icu/ - free slots https://slotgames.icu/ - slots of vegas casino

free penny slots no download vegas slots online hollywood casino online slots free vegas slots | https://slotsrealmoney.icu/ - vegas world free games online slots https://slotsrealmoney.icu/ - play free slots

free penny slots no download vegas slots online hollywood casino online slots free vegas slots | https://slotsrealmoney.icu/ - vegas world free games online slots https://slotsrealmoney.icu/ - play free slots

viagra plus canadian generic viagra viagra brand when will viagra be generic

greektown casino virgin casino online nj chumba casino paradise casino | https://realmoneycasino.ooo/ - mgm online casino nj https://realmoneycasino.ooo/ - caesars online casino

jackpot party casino slots scatter slots cashman casino slots play free slots for fun | https://slotsrealmoney.icu/ - free slots casino games https://slotsrealmoney.icu/ - infinity slots

slots farm world class casino slots gsn casino slots hollywood casino free slots | https://slotgames.icu/ - pch slots https://slotgames.icu/ - wizard of oz slots

lady luck online casino big fish casino online casino slots casino game | [url=https://onlinecasinoplay.us.org/]casino play[/url] [url=https://onlinecasinoplay.us.org/]empire city online casino[/url]

lady luck online casino big fish casino online casino slots casino game | [url=https://onlinecasinoplay.us.org/]casino play[/url] [url=https://onlinecasinoplay.us.org/]empire city online casino[/url]

play casino free casino games slots casino blackjack belterra casino | https://realmoneycasino.ooo/ - oxford casino https://realmoneycasino.ooo/ - bonus casino

play casino free casino games slots casino blackjack belterra casino | https://realmoneycasino.ooo/ - oxford casino https://realmoneycasino.ooo/ - bonus casino

winstar casino oklahoma paradise casino play online casino free online casino | https://playcasinoslots.ooo/ - caesars casino online https://playcasinoslots.ooo/ - play free vegas casino games

winstar casino oklahoma paradise casino play online casino free online casino | https://playcasinoslots.ooo/ - caesars casino online https://playcasinoslots.ooo/ - play free vegas casino games

free video slots vegas world free slots games slots games free free slots games | https://slotsrealmoney.icu/ - slot games https://slotsrealmoney.icu/ - slots lounge

free video slots vegas world free slots games slots games free free slots games | https://slotsrealmoney.icu/ - slot games https://slotsrealmoney.icu/ - slots lounge

free video slots free casino slots no download free online slots doubledown casino free slots | https://slotgames.icu/ - penny slots free online https://slotgames.icu/ - free slots games online

free online slots casino games free online firekeepers casino gsn casino slots | https://onlinecasinoplay.us.org/ - casino games online https://onlinecasinoplay.us.org/ - free slots games

free online slots casino games free online firekeepers casino gsn casino slots | https://onlinecasinoplay.us.org/ - casino games online https://onlinecasinoplay.us.org/ - free slots games

cashman casino slots free buffalo slots hollywood casino online slots simslots free slots | https://slotsrealmoney.icu/ - pop slots casino https://slotsrealmoney.icu/ - slots free online

betfair casino online nj gsn casino slots playmgm nj casino online bovada blackjack | https://playcasinoslots.ooo/ - casino games online https://playcasinoslots.ooo/ - caesars casino online

betfair casino online nj gsn casino slots playmgm nj casino online bovada blackjack | https://playcasinoslots.ooo/ - casino games online https://playcasinoslots.ooo/ - caesars casino online

bigfish casino online games real money casino vegas world casino games treasure island casino minnesota | https://realmoneycasino.ooo/ - mohegan sun casino https://realmoneycasino.ooo/ - parx casino

bigfish casino online games real money casino vegas world casino games treasure island casino minnesota | https://realmoneycasino.ooo/ - mohegan sun casino https://realmoneycasino.ooo/ - parx casino

free slots machines free slots games online heart of vegas slots pch slots tournament | https://slotgames.icu/ - best online slots https://slotgames.icu/ - free slot games with no download

slot games free slot machine games heart of vegas slots vegas free slots online | https://slotsrealmoney.icu/ - online casino slots no download https://slotsrealmoney.icu/ - play free slots

slot games free slot machine games heart of vegas slots vegas free slots online | https://slotsrealmoney.icu/ - online casino slots no download https://slotsrealmoney.icu/ - play free slots

casino games free online big fish casino virgin online casino best online casinos | https://onlinecasinoplay.us.org/ - online casinos https://onlinecasinoplay.us.org/ - free casino games vegas world

casino games free online big fish casino virgin online casino best online casinos | https://onlinecasinoplay.us.org/ - online casinos https://onlinecasinoplay.us.org/ - free casino games vegas world

gsn casino free casino posh casino online caesars online casino | https://onlinecasinoo.icu/ - soaring eagle casino https://onlinecasinoo.icu/ - slot machines

gsn casino free casino posh casino online caesars online casino | https://onlinecasinoo.icu/ - soaring eagle casino https://onlinecasinoo.icu/ - slot machines

monopoly slots vegas slots online free slots free games free vegas slots | https://slotgames.icu/ - play slots online https://slotgames.icu/ - free slots no download no registration

monopoly slots vegas slots online free slots free games free vegas slots | https://slotgames.icu/ - play slots online https://slotgames.icu/ - free slots no download no registration

hard rock casino atlantic city san manuel online casino lady luck mystic lake casino | https://playcasinoslots.ooo/ - free online casino slots https://playcasinoslots.ooo/ - doubleu casino on facebook

hard rock casino atlantic city san manuel online casino lady luck mystic lake casino | https://playcasinoslots.ooo/ - free online casino slots https://playcasinoslots.ooo/ - doubleu casino on facebook

high 5 casino vegas world casino games zone online casino play casino | https://onlinecasinoplay.us.org/ - hypercasinos https://onlinecasinoplay.us.org/ - virgin online casino

slotomania free slots slots free online play free slots free slots | https://slotsrealmoney.icu/ - free slot play no download https://slotsrealmoney.icu/ - house of fun free slots

slotomania free slots slots free online play free slots free slots | https://slotsrealmoney.icu/ - free slot play no download https://slotsrealmoney.icu/ - house of fun free slots

slots for real money big fish casino free online casino games three rivers casino | [url=https://playonlinecasino.icu/]oxford casino[/url] [url=https://playonlinecasino.icu/]cafe casino online[/url]

slots for real money big fish casino free online casino games three rivers casino | [url=https://playonlinecasino.icu/]oxford casino[/url] [url=https://playonlinecasino.icu/]cafe casino online[/url]

free vegas casino games free casino games no download zone online casino slots for real money | https://onlinecasinoplay.us.org/ - borgata online casino https://onlinecasinoplay.us.org/ - no deposit casino

free vegas casino games free casino games no download zone online casino slots for real money | https://onlinecasinoplay.us.org/ - borgata online casino https://onlinecasinoplay.us.org/ - no deposit casino

real casino slots mirrorball slots mohegan sun free online slots buffalo gold slots | https://slotgames.icu/ - penny slots free online https://slotgames.icu/ - free penny slots

real casino slots mirrorball slots mohegan sun free online slots buffalo gold slots | https://slotgames.icu/ - penny slots free online https://slotgames.icu/ - free penny slots

parx casino online hollywood online casino hollywood casino firekeepers casino | https://realmoneycasino.ooo/ - vegas world casino games https://realmoneycasino.ooo/ - mohegan sun casino

Pages

Add new comment