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

zone online casino choctaw casino durant oklahoma double down casino bigfish casino online games | https://realmoneycasino.ooo/ - gsn casino https://realmoneycasino.ooo/ - high five casino slots

zone online casino choctaw casino durant oklahoma double down casino bigfish casino online games | https://realmoneycasino.ooo/ - gsn casino https://realmoneycasino.ooo/ - high five casino slots

grand casino hinckley casinos online gsn casino slots double down casino | [url=https://playcasinoslots.ooo/]caesar casino online slot games[/url] [url=https://playcasinoslots.ooo/]mohegan sun casino[/url]

slots free games free games for casino slots hollywood quick hits free slots vegas slots | https://slotgames.icu/ - cashman casino slots free https://slotgames.icu/ - simslots free slots

vegas world free slots free slots vegas free slots machines slots for free | https://slotsrealmoney.icu/ - vegas world slots https://slotsrealmoney.icu/ - free slots

borgata online casino stn play online casino casino games free free casino games slotomania | https://playonlinecasino.icu/ - caesars online casino https://playonlinecasino.icu/ - casino games slots free

caesars casino online mountaineer casino casino bonus free vegas casino games | https://onlinecasinoo.icu/ - ilani casino https://onlinecasinoo.icu/ - double down casino

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

seneca allegany casino cashman casino zone online casino grand casino hinckley | https://playcasinoslots.ooo/ - usa online casino https://playcasinoslots.ooo/ - chinook winds casino

seneca allegany casino cashman casino zone online casino grand casino hinckley | https://playcasinoslots.ooo/ - usa online casino https://playcasinoslots.ooo/ - chinook winds casino

bovada casino prairie meadows casino casinos near me real casino slots | https://realmoneycasino.ooo/ - river spirit casino tulsa https://realmoneycasino.ooo/ - caesars online casino

bovada casino prairie meadows casino casinos near me real casino slots | https://realmoneycasino.ooo/ - river spirit casino tulsa https://realmoneycasino.ooo/ - caesars online casino

gold fish casino slots free slots games free slots hollywood pch slots tournament | https://slotsrealmoney.icu/ - free slots hollywood https://slotsrealmoney.icu/ - free slots online

gold fish casino slots free slots games free slots hollywood pch slots tournament | https://slotsrealmoney.icu/ - free slots hollywood https://slotsrealmoney.icu/ - free slots online

free casino slot games free casino slots games mirrorball slots vegas casino slots | https://slotgames.icu/ - free online slots https://slotgames.icu/ - free slots with no download or registration

free casino slot games hollywood online casino virgin casino online nj gsn casino slots | https://playonlinecasino.icu/ - muckleshoot casino https://playonlinecasino.icu/ - hard rock casino tampa

free casino slot games hollywood online casino virgin casino online nj gsn casino slots | https://playonlinecasino.icu/ - muckleshoot casino https://playonlinecasino.icu/ - hard rock casino tampa

san manuel casino free vegas slots online casino snoqualmie casino foxwoods online casino | https://onlinecasinoo.icu/ - free casino games slots https://onlinecasinoo.icu/ - caesars online casino

free slots online no download free slots hollywood cashman casino slots free freeslots.com slots | https://slotsrealmoney.icu/ - free games online slots https://slotsrealmoney.icu/ - free online slots no download no registration

free slots online no download free slots hollywood cashman casino slots free freeslots.com slots | https://slotsrealmoney.icu/ - free games online slots https://slotsrealmoney.icu/ - free online slots no download no registration

online casino bonus casino online slots firekeepers casino online casino bonus | https://onlinecasinoplay.us.org/ - free casino games slot machines https://onlinecasinoplay.us.org/ - free casino games slotomania

bonus casino online gambling sites pala casino online nj harrah online casino | https://realmoneycasino.ooo/ - penny slots https://realmoneycasino.ooo/ - golden nugget online casino

bonus casino online gambling sites pala casino online nj harrah online casino | https://realmoneycasino.ooo/ - penny slots https://realmoneycasino.ooo/ - golden nugget online casino

free vegas casino games online casino slots tropicana online casino online gambling sites | https://playcasinoslots.ooo/ - empire city online casino https://playcasinoslots.ooo/ - belterra casino

free vegas casino games online casino slots tropicana online casino online gambling sites | https://playcasinoslots.ooo/ - empire city online casino https://playcasinoslots.ooo/ - belterra casino

liberty slots buffalo gold slots slots free online hollywood casino free slot play | https://slotgames.icu/ - brian christopher slots https://slotgames.icu/ - hearts of vegas free slots

free casino games vegas world free slots games slots for real money empire city casino online | https://playonlinecasino.icu/ - twin river casino https://playonlinecasino.icu/ - casino online

free casino games vegas world free slots games slots for real money empire city casino online | https://playonlinecasino.icu/ - twin river casino https://playonlinecasino.icu/ - casino online

red rock casino free casino games slots casino real money slots for real money | https://onlinecasinoo.icu/ - paradise casino https://onlinecasinoo.icu/ - oxford casino

red rock casino free casino games slots casino real money slots for real money | https://onlinecasinoo.icu/ - paradise casino https://onlinecasinoo.icu/ - oxford casino

discount viagra generic viagra review viagra samples generic viagra online

cashman casino slots free caesars slots free online slots no download no registration doubledown casino free slots | https://slotsrealmoney.icu/ - quick hits free slots https://slotsrealmoney.icu/ - brian christopher slots

cashman casino slots free caesars slots free online slots no download no registration doubledown casino free slots | https://slotsrealmoney.icu/ - quick hits free slots https://slotsrealmoney.icu/ - brian christopher slots

jackpot party casino muckleshoot casino grand falls casino casino online | https://realmoneycasino.ooo/ - hollywood casino online slots https://realmoneycasino.ooo/ - gambling sites

jackpot party casino muckleshoot casino grand falls casino casino online | https://realmoneycasino.ooo/ - hollywood casino online slots https://realmoneycasino.ooo/ - gambling sites

slotomania slot machines borgata hotel casino online gambling casino chinook winds casino | https://playcasinoslots.ooo/ - casino bonus https://playcasinoslots.ooo/ - hypercasinos

slotomania slot machines borgata hotel casino online gambling casino chinook winds casino | https://playcasinoslots.ooo/ - casino bonus https://playcasinoslots.ooo/ - hypercasinos

vegas world free games online slots caesars free slots my vegas slots hollywood casino free slots | https://slotgames.icu/ - las vegas free slots https://slotgames.icu/ - goldfish casino slots free

vegas world free games online slots caesars free slots my vegas slots hollywood casino free slots | https://slotgames.icu/ - las vegas free slots https://slotgames.icu/ - goldfish casino slots free

doubleu casino free casino games sun moon red rock casino casinos in iowa | https://playonlinecasino.icu/ - zone online casino https://playonlinecasino.icu/ - turning stone online casino

doubleu casino free casino games sun moon red rock casino casinos in iowa | https://playonlinecasino.icu/ - zone online casino https://playonlinecasino.icu/ - turning stone online casino

lady luck borgata online casino online casino games online casino bonus | https://onlinecasinoplay.us.org/ - slots for real money https://onlinecasinoplay.us.org/ - lady luck online casino

lady luck borgata online casino online casino games online casino bonus | https://onlinecasinoplay.us.org/ - slots for real money https://onlinecasinoplay.us.org/ - lady luck online casino

cashman casino slots slot machine games free vegas world free slots real casino slots | https://slotgames.icu/ - vegas slots online https://slotgames.icu/ - charlestown races and slots

parx casino free vegas slots online casino doubleu casino on facebook mystic lake casino | https://realmoneycasino.ooo/ - free online slots https://realmoneycasino.ooo/ - virgin casino online

parx casino free vegas slots online casino doubleu casino on facebook mystic lake casino | https://realmoneycasino.ooo/ - free online slots https://realmoneycasino.ooo/ - virgin casino online

chinook winds casino casino bonus cafe casino online big fish casino slots | https://playcasinoslots.ooo/ - firekeepers casino https://playcasinoslots.ooo/ - free casino games sun moon

chinook winds casino casino bonus cafe casino online big fish casino slots | https://playcasinoslots.ooo/ - firekeepers casino https://playcasinoslots.ooo/ - free casino games sun moon

caesars slots vegas free slots casino slots free games free slot games | https://slotsrealmoney.icu/ - hollywood casino free slot play https://slotsrealmoney.icu/ - vegas free slots

caesars slots vegas free slots casino slots free games free slot games | https://slotsrealmoney.icu/ - hollywood casino free slot play https://slotsrealmoney.icu/ - vegas free slots

Pages

Add new comment