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

casino bonus gsn casino slots online gambling casino bovada casino | [url=https://onlinecasinoslotsmaxx.com/]play casino[/url] [url=https://onlinecasinoslotsmaxx.com/]free online casino slots[/url]

online casino real money online casino gsn casino slots online casino | [url=https://onlinecasino2018.us.org/]online casino gambling[/url] [url=https://onlinecasino2018.us.org/]casino online[/url]

online gambling casino slots gsn casino slots online casino bonus | [url=https://onlinecasinoplay.us.org/]gsn casino games[/url] [url=https://onlinecasinoplay.us.org/]online casinos for us players[/url]

mgm online casino real money casino casino slots online casino real money | [url=https://onlinecasinoslotsmaxx.com/]online gambling casino[/url] [url=https://onlinecasinoslotsmaxx.com/]casino real money[/url]

mgm online casino real money casino casino slots online casino real money | [url=https://onlinecasinoslotsmaxx.com/]online gambling casino[/url] [url=https://onlinecasinoslotsmaxx.com/]casino real money[/url]

casino play online casino real money real money casino casino bonus | [url=https://onlinecasino2018.us.org/]best online casinos[/url] [url=https://onlinecasino2018.us.org/]casino bonus[/url]

online casino bonus mgm online casino online casino games online gambling casino | [url=https://onlinecasinoplay.us.org/]online casino[/url] [url=https://onlinecasinoplay.us.org/]casino games[/url]

casino online play casino online casino games casino games | [url=https://onlinecasinoslotsmaxx.com/]casino online[/url] [url=https://onlinecasinoslotsmaxx.com/]mgm online casino[/url]

casino blackjack betfair online casino online casino slots online casino real money | [url=https://onlinecasino2018.us.org/]gsn casino slots[/url] [url=https://onlinecasino2018.us.org/]casino slots[/url]

online casinos for us players free online casino slots online casino best online casino | [url=https://onlinecasinoslotsmaxx.com/]online casinos[/url] [url=https://onlinecasinoslotsmaxx.com/]best online casinos[/url]

online casino slots real money casino free online casino slots slots for real money | [url=https://onlinecasino2018.us.org/]casino play[/url] [url=https://onlinecasino2018.us.org/]slots for real money[/url]

online casino slots real money casino free online casino slots slots for real money | [url=https://onlinecasino2018.us.org/]casino play[/url] [url=https://onlinecasino2018.us.org/]slots for real money[/url]

online casino real money casino bonus best online casinos online gambling | [url=https://onlinecasinoplay.us.org/]casino play[/url] [url=https://onlinecasinoplay.us.org/]betfair online casino[/url]

bovada casino online casino slots online casino gambling free online casino slots | [url=https://onlinecasino2018.us.org/]casino real money[/url] [url=https://onlinecasino2018.us.org/]online casino slots[/url]

best online casinos online casino bonus gsn casino gsn casino | [url=https://onlinecasinoplay.us.org/]play casino[/url] [url=https://onlinecasinoplay.us.org/]casino blackjack[/url]

online gambling casino real money online casino bonus casino real money | [url=https://onlinecasinoslotsmaxx.com/]best online casino[/url] [url=https://onlinecasinoslotsmaxx.com/]best online casinos[/url]

online gambling casino real money online casino bonus casino real money | [url=https://onlinecasinoslotsmaxx.com/]best online casino[/url] [url=https://onlinecasinoslotsmaxx.com/]best online casinos[/url]

betfair online casino online casino games slots for real money online casino bonus | [url=https://onlinecasino2018.us.org/]online casinos[/url] [url=https://onlinecasino2018.us.org/]casino games[/url]

best online casino online gambling casino mgm online casino online casinos | [url=https://onlinecasinoplay.us.org/]slots for real money[/url] [url=https://onlinecasinoplay.us.org/]online gambling casino[/url]

online casino games gsn casino games gsn casino slots online gambling | [url=https://onlinecasinoslotsmaxx.com/]free online casino slots[/url] [url=https://onlinecasinoslotsmaxx.com/]casino play[/url]

online casino games gsn casino games gsn casino slots online gambling | [url=https://onlinecasinoslotsmaxx.com/]free online casino slots[/url] [url=https://onlinecasinoslotsmaxx.com/]casino play[/url]

gsn casino games best online casino bovada casino online casinos | [url=https://onlinecasino2018.us.org/]online gambling casino[/url] [url=https://onlinecasino2018.us.org/]casino games[/url]

online casino games online gambling online casino slots casino slots | [url=https://onlinecasino2018.us.org/]casino online[/url] [url=https://onlinecasino2018.us.org/]betfair online casino[/url]

mgm online casino online casino games casino online slots for real money | [url=https://onlinecasinoslotsmaxx.com/]best online casino[/url] [url=https://onlinecasinoslotsmaxx.com/]casino slots[/url]

online casino games real money casino free online casino slots casino real money | [url=https://onlinecasino2018.us.org/]online casino real money[/url] [url=https://onlinecasino2018.us.org/]casino bonus[/url]

gsn casino gsn casino gsn casino slots online casinos | [url=https://onlinecasino2018.us.org/]casino games[/url] [url=https://onlinecasino2018.us.org/]gsn casino slots[/url]

gsn casino gsn casino gsn casino slots online casinos | [url=https://onlinecasino2018.us.org/]casino games[/url] [url=https://onlinecasino2018.us.org/]gsn casino slots[/url]

free online casino slots online casinos for us players online casinos online casinos | [url=https://onlinecasinoslotsmaxx.com/]casino real money[/url] [url=https://onlinecasinoslotsmaxx.com/]play casino[/url]

casino bonus casino slots casino blackjack online casinos for us players | [url=https://onlinecasino2018.us.org/]casino games[/url] [url=https://onlinecasino2018.us.org/]online casinos for us players[/url]

casino bonus casino slots casino blackjack online casinos for us players | [url=https://onlinecasino2018.us.org/]casino games[/url] [url=https://onlinecasino2018.us.org/]online casinos for us players[/url]

betfair online casino free online casino slots mgm online casino casino slots | [url=https://onlinecasinoslotsmaxx.com/]online casinos[/url] [url=https://onlinecasinoslotsmaxx.com/]play casino[/url]

online casino real money casino slots best online casino casino play | [url=https://onlinecasino2018.us.org/]casino blackjack[/url] [url=https://onlinecasino2018.us.org/]casino blackjack[/url]

real money casino gsn casino online casino gambling casino bonus | [url=https://onlinecasino2018.us.org/]best online casinos[/url] [url=https://onlinecasino2018.us.org/]online casino real money[/url]

slots for real money casino blackjack online gambling casino casino play | [url=https://onlinecasinoslotsmaxx.com/]real money casino[/url] [url=https://onlinecasinoslotsmaxx.com/]online gambling casino[/url]

online gambling casino gsn casino games free online casino slots casino play | [url=https://onlinecasino2018.us.org/]casino online[/url] [url=https://onlinecasino2018.us.org/]online gambling casino[/url]

online casinos casino real money casino play online gambling | [url=https://onlinecasinoslotsmaxx.com/]gsn casino games[/url] [url=https://onlinecasinoslotsmaxx.com/]online casino bonus[/url]

online casinos casino real money casino play online gambling | [url=https://onlinecasinoslotsmaxx.com/]gsn casino games[/url] [url=https://onlinecasinoslotsmaxx.com/]online casino bonus[/url]

slots for real money mgm online casino online casinos slots for real money | [url=https://onlinecasino2018.us.org/]online casino games[/url] [url=https://onlinecasino2018.us.org/]gsn casino slots[/url]

casino online gsn casino online casino bonus best online casinos | [url=https://onlinecasino2018.us.org/]gsn casino slots[/url] [url=https://onlinecasino2018.us.org/]online casino real money[/url]

casino play online casino bonus free online casino slots online casino real money | [url=https://onlinecasinoslotsmaxx.com/]casino games[/url] [url=https://onlinecasinoslotsmaxx.com/]online casino gambling[/url]

casino play online casino bonus free online casino slots online casino real money | [url=https://onlinecasinoslotsmaxx.com/]casino games[/url] [url=https://onlinecasinoslotsmaxx.com/]online casino gambling[/url]

bovada casino online casino games online casinos for us players gsn casino | [url=https://onlinecasino2018.us.org/]online gambling[/url] [url=https://onlinecasino2018.us.org/]best online casinos[/url]

casino games slots for real money online casino online casinos | [url=https://onlinecasinoslotsmaxx.com/]online casino real money[/url] [url=https://onlinecasinoslotsmaxx.com/]online casino[/url]

gsn casino slots casino real money online casino games slots for real money | [url=https://onlinecasino2018.us.org/]online casino real money[/url] [url=https://onlinecasino2018.us.org/]online casinos[/url]

Pages

Add new comment