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

house of fun slots hollywood slots vegas world slots slots lounge | https://slotgames.icu/ - free slots 777 https://slotgames.icu/ - wheel of fortune slots

house of fun slots hollywood slots vegas world slots slots lounge | https://slotgames.icu/ - free slots 777 https://slotgames.icu/ - wheel of fortune slots

best online casino online casino games lady luck online casino vegas world casino games | https://onlinecasinoplay.us.org/ - firekeepers casino https://onlinecasinoplay.us.org/ - free online casino

best online casino online casino games lady luck online casino vegas world casino games | https://onlinecasinoplay.us.org/ - firekeepers casino https://onlinecasinoplay.us.org/ - free online casino

price viagra when will viagra be generic low price viagra [url=https://aluixnetwork.com/]cheap viagra online canadian pharmacy[/url]

price viagra when will viagra be generic low price viagra [url=https://aluixnetwork.com/]cheap viagra online canadian pharmacy[/url]

casino real money gsn casino slots slotomania slot machines gun lake casino | https://onlinecasinoo.icu/ - blue chip casino https://onlinecasinoo.icu/ - hollywood casino

casino real money gsn casino slots slotomania slot machines gun lake casino | https://onlinecasinoo.icu/ - blue chip casino https://onlinecasinoo.icu/ - hollywood casino

san manuel casino free vegas casino games bovada blackjack treasure island casino minnesota | https://playcasinogames.icu/ - river spirit casino tulsa https://playcasinogames.icu/ - paradise casino

tropicana online casino free casino games sun moon dakota sioux casino mgm online casino | https://casino-bonus.icu/ - pala casino online nj https://casino-bonus.icu/ - lady luck online casino

slots free games gold fish casino slots online slot games hollywood casino free slot play | https://slotonline.icu/ - slots of vegas casino https://slotonline.icu/ - online slots

slot games slots games free slots vegas world buffalo gold slots | https://playslotsonline.icu/ - las vegas free slots https://playslotsonline.icu/ - vegas slots online

free casino games online online gambling sites free vegas slots online casino online slot machines | https://onlinecasinorealmoney.ooo/ - hyper casinos https://onlinecasinorealmoney.ooo/ - free slots games

free casino games online online gambling sites free vegas slots online casino online slot machines | https://onlinecasinorealmoney.ooo/ - hyper casinos https://onlinecasinorealmoney.ooo/ - free slots games

four winds casino sugarhouse online casino doubleu casino on facebook slot machines | https://playslots.ooo/ - winstar world casino https://playslots.ooo/ - cashman casino

free casino games slotomania virgin casino online doubledown casino promo codes seneca niagara casino | https://onlinecasinorealmoney.ooo/ - free online casino slots https://onlinecasinorealmoney.ooo/ - online casino

free slot play no download free online slots vegas world slots free online casino slots | https://playslotsonline.icu/ - vegas world free slots games https://playslotsonline.icu/ - online casino slots no download

online casino gambling turning stone online casino betfair casino online nj soaring eagle casino | https://playcasinogames.icu/ - mohegan sun casino https://playcasinogames.icu/ - bonus casino

hallmark casino online winstar world casino gsn casino games oxford casino | https://playslots.ooo/ - winstar casino oklahoma https://playslots.ooo/ - paradise casino

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

free slots games online online slots online slots free online slots free | https://slotonline.icu/ - pch slots https://slotonline.icu/ - jackpot party casino slots

free slots games online online slots online slots free online slots free | https://slotonline.icu/ - pch slots https://slotonline.icu/ - jackpot party casino slots

zone online casino hollywood casino online slots doubleu casino online casino reviews | https://casino-bonus.icu/ - high 5 casino https://casino-bonus.icu/ - casino online slots

zone online casino hollywood casino online slots doubleu casino online casino reviews | https://casino-bonus.icu/ - high 5 casino https://casino-bonus.icu/ - casino online slots

hot shot casino slots jackpot magic slots free slot games online vegas casino slots | https://playslotsonline.icu/ - mirrorball slots https://playslotsonline.icu/ - liberty slots casino

casino blackjack pechanga casino del lago casino oxford casino | https://onlinecasinorealmoney.ooo/ - online gambling casino https://onlinecasinorealmoney.ooo/ - casino games slots free

casino blackjack pechanga casino del lago casino oxford casino | https://onlinecasinorealmoney.ooo/ - online gambling casino https://onlinecasinorealmoney.ooo/ - casino games slots free

double u casino lady luck vegas casino games virgin online casino | https://playslots.ooo/ - bovada casino https://playslots.ooo/ - doubleu casino

free slots vegas world buffalo gold slots jackpot party casino slots free slots | https://slotonline.icu/ - cleopatra slots https://slotonline.icu/ - free slot play

online casino fortune bay casino bigfish casino online games free casino games online | https://casino-bonus.icu/ - play casino https://casino-bonus.icu/ - online gambling

free casino games island view casino grand casino hinckley free slots games | https://playcasinogames.icu/ - downstream casino https://playcasinogames.icu/ - online casino reviews

free casino games island view casino grand casino hinckley free slots games | https://playcasinogames.icu/ - downstream casino https://playcasinogames.icu/ - online casino reviews

casino online caesars online casino casino game | https://onlinecasino888.us.org/ - online casino gambling

casino online caesars online casino casino game | https://onlinecasino888.us.org/ - online casino gambling

huuuge casino best online casinos casinos in iowa online casino real money | https://onlinecasinorealmoney.ooo/ - hallmark casino online https://onlinecasinorealmoney.ooo/ - gsn casino

huuuge casino best online casinos casinos in iowa online casino real money | https://onlinecasinorealmoney.ooo/ - hallmark casino online https://onlinecasinorealmoney.ooo/ - gsn casino

free slot games online free penny slots online wheel of fortune slots slot games | https://playslotsonline.icu/ - free online slots no download https://playslotsonline.icu/ - play slots

free slot games online free penny slots online wheel of fortune slots slot games | https://playslotsonline.icu/ - free online slots no download https://playslotsonline.icu/ - play slots

empire city online casino virgin online casino online casino games free twin river casino | https://playslots.ooo/ - slots for real money https://playslots.ooo/ - real casino

slots of vegas casino hollywood casino online slots free free slots casino games online slot games | https://slotonline.icu/ - charlestown races and slots https://slotonline.icu/ - free online slots

slots of vegas casino hollywood casino online slots free free slots casino games online slot games | https://slotonline.icu/ - charlestown races and slots https://slotonline.icu/ - free online slots

online casino bonus paradise casino riverwind casino paradise casino | https://casino-bonus.icu/ - online casino bonus https://casino-bonus.icu/ - borgata online casino

online casino bonus paradise casino riverwind casino paradise casino | https://casino-bonus.icu/ - online casino bonus https://casino-bonus.icu/ - borgata online casino

dakota sioux casino mohegan sun casino casinos online hallmark casino online | https://playcasinogames.icu/ - casinos online https://playcasinogames.icu/ - golden nugget online casino

dakota sioux casino mohegan sun casino casinos online hallmark casino online | https://playcasinogames.icu/ - casinos online https://playcasinogames.icu/ - golden nugget online casino

belterra casino soaring eagle casino free casino real money casino | https://onlinecasinorealmoney.ooo/ - free slots games https://onlinecasinorealmoney.ooo/ - online casino gambling

belterra casino soaring eagle casino free casino real money casino | https://onlinecasinorealmoney.ooo/ - free slots games https://onlinecasinorealmoney.ooo/ - online casino gambling

slot machine games free free slot machine games las vegas free slots online slots free | https://playslotsonline.icu/ - da vinci diamonds free online slots https://playslotsonline.icu/ - freeslots.com slots

slot machine games free free slot machine games las vegas free slots online slots free | https://playslotsonline.icu/ - da vinci diamonds free online slots https://playslotsonline.icu/ - freeslots.com slots

winstar world casino winstar casino san manuel casino caesars casino | https://playslots.ooo/ - casino slots https://playslots.ooo/ - empire city casino

Pages

Add new comment