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

cialis online generic cialis reviews cialis 20mg when will cialis go generic

viagra 50mg cheap viagra online generic viagra online pharmacy generic viagra reviews

viagra 50mg cheap viagra online generic viagra online pharmacy generic viagra reviews

cialis for sale cialis online generic cialis buy cialis

non prescription viagra best place to buy viagra online viagra jelly canadian pharmacy generic viagra

cialis generic tadalafil tadalafil citrate buy cialis online

cialis generic tadalafil tadalafil citrate buy cialis online

buy viagra soft cheap viagra online buy viagra generic viagra for sale

viagra and alcohol online viagra viagra wholesale generic viagra online

viagra and alcohol online viagra viagra wholesale generic viagra online

order viagra online cheap viagra online order generic viagra viagra online usa

order viagra online cheap viagra online order generic viagra viagra online usa

online viagra viagra online cheap cheap viagra pills viagra online prescription

online viagra viagra online cheap cheap viagra pills viagra online prescription

viagra online without prescription cheapest generic viagra natural viagra substitutes generic viagra reviews

viagra online without prescription cheapest generic viagra natural viagra substitutes generic viagra reviews

free viagra sample cheapest viagra online viagra alternative viagra online canadian pharmacy

free viagra sample cheapest viagra online viagra alternative viagra online canadian pharmacy

try viagra for free generic viagra viagra canada canadian generic viagra

natural alternative to viagra buy generic viagra online women viagra generic viagra canada

natural alternative to viagra buy generic viagra online women viagra generic viagra canada

cialis reviews cialis online usa cialis daily buy cialis online safely

generic viagra buy viagra online reviews viagra brand best place to buy generic viagra online

viagra prices viagra online generic what is viagra cheap viagra

viagra prices viagra online generic what is viagra cheap viagra

buying viagra online when will viagra go generic order viagra online viagra

buying viagra online when will viagra go generic order viagra online viagra

viagra and alcohol cheap viagra online canadian pharmacy sildenafil viagra order viagra online

viagra and alcohol cheap viagra online canadian pharmacy sildenafil viagra order viagra online

buy cialis without prescription buy cialis cheap generic cialis online were can i buy cialis

buy cialis without prescription buy cialis cheap generic cialis online were can i buy cialis

tadalafil cialis buy generic cialis online cialis pills best place to buy cialis online reviews

purchase cialis cialis generic tadalafil online order cialis online

viagra soft tablets online viagra reviews viagra for sale online viagra

viagra soft tablets online viagra reviews viagra for sale online viagra

what does viagra do buy viagra online reviews sublingual viagra buy viagra online cheapest

what does viagra do buy viagra online reviews sublingual viagra buy viagra online cheapest

viagra plus buy viagra cheap viagra cost pfizer viagra online

viagra plus buy viagra cheap viagra cost pfizer viagra online

buy brand viagra viagra online cheap viagra sildenafil where to buy viagra online

buy brand viagra viagra online cheap viagra sildenafil where to buy viagra online

cialis canada buy cialis online cheap cialis price when does cialis go generic

cialis canada buy cialis online cheap cialis price when does cialis go generic

cheap viagra 100mg buy generic viagra online canada viagra cheap viagra online

buy viagra no prescription online viagra prescription viagra viagra online generic

cheap viagra best generic viagra natural alternative to viagra best generic viagra

cheap viagra best generic viagra natural alternative to viagra best generic viagra

Pages

Add new comment