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

online casinos for us players online casino slots casino online online gambling | [url=https://onlinecasinoplay777.us.org/]casino play[/url] [url=https://onlinecasinoplay777.us.org/]online casino bonus[/url]

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

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

casino real money real money casino gsn casino real money casino | [url=https://onlinecasinoslotsmaxx.com/]gsn casino games[/url] [url=https://onlinecasinoslotsmaxx.com/]bovada casino[/url]

casino real money real money casino gsn casino real money casino | [url=https://onlinecasinoslotsmaxx.com/]gsn casino games[/url] [url=https://onlinecasinoslotsmaxx.com/]bovada casino[/url]

best online casinos casino games online gambling casino betfair online casino | [url=https://onlinecasinoplay777.us.org/]mgm online casino[/url] [url=https://onlinecasinoplay777.us.org/]online casino bonus[/url]

best online casinos casino games online gambling casino betfair online casino | [url=https://onlinecasinoplay777.us.org/]mgm online casino[/url] [url=https://onlinecasinoplay777.us.org/]online casino bonus[/url]

online casino gambling online casino real money gsn casino games online casino games - [url=https://onlinecasinoplay.us.org/]casino bonus[/url] - [url=https://onlinecasinoplay.us.org/]free online casino slots[/url]

online casino gambling online casino real money gsn casino games online casino games - [url=https://onlinecasinoplay.us.org/]casino bonus[/url] - [url=https://onlinecasinoplay.us.org/]free online casino slots[/url]

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

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

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

casino games casino play casino blackjack best online casino | [url=https://onlinecasinoplay777.us.org/]online gambling casino[/url] [url=https://onlinecasinoplay777.us.org/]online casino gambling[/url]

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

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

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

casino online play casino casino play play casino - [url=https://onlinecasinoplay.us.org/]online casino slots[/url] - [url=https://onlinecasinoplay.us.org/]online casino slots[/url]

casino online play casino casino play play casino - [url=https://onlinecasinoplay.us.org/]online casino slots[/url] - [url=https://onlinecasinoplay.us.org/]online casino slots[/url]

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

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

online casino slots casino online real money casino real money casino - [url=https://onlinecasinoplay.us.org/]online casino slots[/url] - [url=https://onlinecasinoplay.us.org/]gsn casino[/url]

online casino slots casino online real money casino real money casino - [url=https://onlinecasinoplay.us.org/]online casino slots[/url] - [url=https://onlinecasinoplay.us.org/]gsn casino[/url]

mgm online casino slots for real money online casinos online casinos | [url=https://online-casinos.us.org/]online casinos for us players[/url] [url=https://online-casinos.us.org/]online casino bonus[/url]

mgm online casino slots for real money online casinos online casinos | [url=https://online-casinos.us.org/]online casinos for us players[/url] [url=https://online-casinos.us.org/]online casino bonus[/url]

best online casino real money casino mgm online casino online casino bonus | [url=https://online-casinos.us.org/]casino real money[/url] [url=https://online-casinos.us.org/]free online casino slots[/url]

best online casino real money casino mgm online casino online casino bonus | [url=https://online-casinos.us.org/]casino real money[/url] [url=https://online-casinos.us.org/]free online casino slots[/url]

mgm online casino online casino games slots for real money best online casinos | [url=https://online-casinos.us.org/]play casino[/url] [url=https://online-casinos.us.org/]bovada casino[/url]

casino games bovada casino gsn casino slots mgm online casino - [url=https://onlinecasinoplay.us.org/]play casino[/url] - [url=https://onlinecasinoplay.us.org/]best online casinos[/url]

casino games bovada casino gsn casino slots mgm online casino - [url=https://onlinecasinoplay.us.org/]play casino[/url] - [url=https://onlinecasinoplay.us.org/]best online casinos[/url]

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

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

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

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

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

casino real money casino slots online casino games gsn casino slots - [url=https://onlinecasinoplay.us.org/]real money casino[/url] - [url=https://onlinecasinoplay.us.org/]online casino games[/url]

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

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

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

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

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

online casino bovada casino online casinos for us players online gambling | [url=https://online-casinos.us.org/]play casino[/url] [url=https://online-casinos.us.org/]online casinos[/url]

online casino bovada casino online casinos for us players online gambling | [url=https://online-casinos.us.org/]play casino[/url] [url=https://online-casinos.us.org/]online casinos[/url]

bovada casino online casino gambling best online casino online casino gambling - [url=https://onlinecasinoplay.us.org/]online casino real money[/url] - [url=https://onlinecasinoplay.us.org/]online gambling[/url]

bovada casino online casino gambling best online casino online casino gambling - [url=https://onlinecasinoplay.us.org/]online casino real money[/url] - [url=https://onlinecasinoplay.us.org/]online gambling[/url]

Pages

Add new comment