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

discount cialis generic cialis tadalafil liquid best place to buy cialis online reviews

discount cialis generic cialis tadalafil liquid best place to buy cialis online reviews

viagra tablets for sale best place to buy viagra online low price viagra canadian online pharmacy viagra

viagra tablets for sale best place to buy viagra online low price viagra canadian online pharmacy viagra

tadalafil 20 mg cialis online cialis online no prescription cialis online

cialis 20mg п»їbuy cheap cialis order cialis online online cialis

cialis 20mg п»їbuy cheap cialis order cialis online online cialis

cialis 20 mg online cialis generic tadalafil buying cialis online safely

cialis 20 mg online cialis generic tadalafil buying cialis online safely

what is tadalafil cheap cialis online canadian pharmacy generic cialis online where to buy cialis online

what is tadalafil cheap cialis online canadian pharmacy generic cialis online where to buy cialis online

viagra sildenafil viagra online without prescription best generic viagra buy viagra online reviews

cialis professional cialis online cheap cialis daily cialis online canada

natural viagra alternatives generic viagra online pharmacy viagra pharmacy viagra online canada

natural viagra alternatives generic viagra online pharmacy viagra pharmacy viagra online canada

try viagra for free when will generic viagra be available online viagra viagra generic

buy brand viagra online pharmacy viagra what does viagra do cheap viagra online canadian pharmacy

buy brand viagra online pharmacy viagra what does viagra do cheap viagra online canadian pharmacy

viagra on line cheap viagra 100mg viagra suppliers how to get viagra cheap

viagra on line cheap viagra 100mg viagra suppliers how to get viagra cheap

viagra without a prescription viagra generic name viagra coupons when does viagra go generic

get viagra how to buy viagra online natural alternative to viagra buy cheap viagra on line

get viagra how to buy viagra online natural alternative to viagra buy cheap viagra on line

viagra soft pills is there a generic viagra viagra and alcohol generic viagra india

viagra soft pills is there a generic viagra viagra and alcohol generic viagra india

generic viagra india generic viagra without a doctor prescription how long does viagra last generic viagra online pharmacy

generic viagra india generic viagra without a doctor prescription how long does viagra last generic viagra online pharmacy

side effects of viagra viagra online prescription free viagra samples cheap viagra free shipping

viagra coupons generic viagra names buy discount viagra generic for viagra

generic viagra india cheap viagra from india free viagra without prescription cheap viagra online canadian pharmacy

side effects of viagra cheap viagra canada buy viagra new york buy viagra online

side effects of viagra cheap viagra canada buy viagra new york buy viagra online

viagra overnight best place to buy viagra online soft tabs viagra when will viagra go generic

viagra overnight best place to buy viagra online soft tabs viagra when will viagra go generic

buy cialis without prescription buying cialis online safely tadalafil buy cialis online cheap

buy cialis without prescription buying cialis online safely tadalafil buy cialis online cheap

viagra brand cheap viagra online viagra generic viagra online

viagra brand cheap viagra online viagra generic viagra online

tadalafil tablets 20 mg is there a generic cialis discount cialis buy cialis online

viagra stories online generic viagra womens viagra when will viagra go generic

viagra stories online generic viagra womens viagra when will viagra go generic

get viagra when will generic viagra be available buy viagra online buy viagra online usa

get viagra when will generic viagra be available buy viagra online buy viagra online usa

mail order viagra viagra online viagra purchase when will viagra be generic

mail order viagra viagra online viagra purchase when will viagra be generic

low price viagra generic viagra online online pharmacy viagra buy generic viagra online

low price viagra generic viagra online online pharmacy viagra buy generic viagra online

cialis without prescription order cialis online cialis tadalafil buy generic cialis online

do you need a prescription for cialis cialis online canada cialis 20 mg online pharmacy cialis

Pages

Add new comment