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

buy generic viagra online online viagra generic viagra generic viagra without a doctor prescription

best generic viagra generic viagra online herbal alternative viagra generic viagra

best generic viagra generic viagra online herbal alternative viagra generic viagra

buy viagra viagra prescription online where to buy viagra online online doctor prescription for viagra

buy viagra viagra prescription online where to buy viagra online online doctor prescription for viagra

no prescription cialis when does cialis go generic cialis online no prescription buy generic cialis online

no prescription cialis when does cialis go generic cialis online no prescription buy generic cialis online

cheap tadalafil cialis discount cialis online cialis

cheap tadalafil cialis discount cialis online cialis

free viagra sample order viagra online sample viagra generic viagra online pharmacy

do you need a prescription for cialis buy cialis cheapest cialis buy cialis

do you need a prescription for cialis buy cialis cheapest cialis buy cialis

cialis online no prescription where to buy cialis over the counter order cialis online buy cialis online overnight shipping

buy viagra where to buy viagra online viagra for sale buy generic viagra online

buy viagra where to buy viagra online viagra for sale buy generic viagra online

buy viagra soft buy generic viagra generic viagra sildenafil online pharmacy viagra

buy viagra soft buy generic viagra generic viagra sildenafil online pharmacy viagra

viagra wholesale generic viagra india purchase viagra buying viagra online

viagra wholesale generic viagra india purchase viagra buying viagra online

cialis online cialis cialis 5mg buy cialis

cialis online cialis cialis 5mg buy cialis

buy tadalafil 20mg price buy cialis online tadalafil dosage buy cialis online

viagra professional viagra prescription online viagra online without prescription viagra prescription online

cheap viagra online viagra online prescription free cheap viagra uk viagra online prescription free

cheap viagra online viagra online prescription free cheap viagra uk viagra online prescription free

viagra online online pharmacy viagra free viagra sample buy generic viagra

viagra online online pharmacy viagra free viagra sample buy generic viagra

cheap viagra from india buy generic viagra how viagra works buy generic viagra online

cheap viagra from india buy generic viagra how viagra works buy generic viagra online

tadalafil 20 mg online cialis cialis without prescription buy cialis online

tadalafil 20 mg online cialis cialis without prescription buy cialis online

tadalafil п»їcialis online cialis for sale cialis online

cheapest viagra prices buy viagra online usa viagra alternatives generic viagra online

cheapest viagra prices buy viagra online usa viagra alternatives generic viagra online

cheap viagra online generic viagra online reviews female viagra pill is there a generic for viagra

cheap viagra online generic viagra online reviews female viagra pill is there a generic for viagra

what does viagra do online doctor prescription for viagra buy viagra online viagra online canada

what does viagra do online doctor prescription for viagra buy viagra online viagra online canada

tadalafil online your domain name cialis cialis

tadalafil online your domain name cialis cialis

cheapest cialis were can i buy cialis cialis daily cheap cialis online

cheapest cialis were can i buy cialis cialis daily cheap cialis online

cialis no prescription were can i buy cialis cialis soft buy cialis online safely

tadalafil reviews buy cialis canada tadalafil 20 mg where to buy cialis online

tadalafil reviews buy cialis canada tadalafil 20 mg where to buy cialis online

order cialis online cialis tadalafil tablets generic cialis reviews

order cialis online cialis tadalafil tablets generic cialis reviews

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

Pages

Add new comment