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

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

cialis prescription cialis online no prescription cialis brand cheap cialis online

cialis prescription cialis online no prescription cialis brand cheap cialis online

generic tadalafil best place to buy cialis online cialis 5mg buy cialis online safely

generic tadalafil best place to buy cialis online cialis 5mg buy cialis online safely

cheap tadalafil buying cheap cialis online tadalafil reviews order cialis online

cheap tadalafil buying cheap cialis online tadalafil reviews order cialis online

cialis online no prescription cialis cialis brand online cialis

cialis online no prescription cialis cialis brand online cialis

liquid tadalafil generic for cialis buy tadalafil online buy cialis online reddit

liquid tadalafil generic for cialis buy tadalafil online buy cialis online reddit

buy viagra soft pfizer viagra online cheap viagra cheap viagra india

buy viagra soft pfizer viagra online cheap viagra cheap viagra india

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

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

cialis without prescription best place to buy cialis online tadalafil cialis cialis generic online

cialis without prescription best place to buy cialis online tadalafil cialis cialis generic online

buy cialis without prescription cialis cialis pills п»їcheap tadalafil

buy cialis without prescription cialis cialis pills п»їcheap tadalafil

buy viagra professional buying viagra online legal real viagra for sale can you buy viagra online

buy viagra professional buying viagra online legal real viagra for sale can you buy viagra online

cheap tadalafil online cialis cialis pills cialis online

cheap tadalafil online cialis cialis pills cialis online

cialis prescription best place to buy cialis online cialis reviews cialis generic date

cialis prescription best place to buy cialis online cialis reviews cialis generic date

cialis price buying cialis online safely tadalafil reviews buy generic cialis

cialis price buying cialis online safely tadalafil reviews buy generic cialis

tadalafil 20mg cialis buy cialis cost cialis online

tadalafil 20mg cialis buy cialis cost cialis online

liquid tadalafil cialis generic cialis online pharmacy buy cialis cheap

liquid tadalafil cialis generic cialis online pharmacy buy cialis cheap

buy tadalafil where to buy cialis over the counter tadalafil buy generic cialis online

buy tadalafil where to buy cialis over the counter tadalafil buy generic cialis online

do you need a prescription for cialis generic cialis cheap cialis generic cialis online

do you need a prescription for cialis generic cialis cheap cialis generic cialis online

cialis soft tabs п»їcialis price cialis no prescription п»їtadalafil reviews

buy cheap cialis buy generic cialis discount cialis buy cialis online

canadian viagra buy viagra online cheap free viagra where can i buy viagra online

canadian viagra buy viagra online cheap free viagra where can i buy viagra online

viagra brand name buy viagra online cheapest buy brand viagra buying viagra online legal

non prescription cialis buy cialis online safely do you need a prescription for cialis buy cialis cheap

viagra canada buy viagra online cheap viagra online no prescription viagra online pharmacy

no prescription cialis cialis generic best price cialis price when will cialis go generic

no prescription cialis cialis generic best price cialis price when will cialis go generic

female viagra pill online doctor prescription for viagra viagra uk sales viagra online usa

female viagra pill online doctor prescription for viagra viagra uk sales viagra online usa

what would happen if a girl took viagra viagra online no prior prescription generic viagra soft tabs viagra online canadian pharmacy

what would happen if a girl took viagra viagra online no prior prescription generic viagra soft tabs viagra online canadian pharmacy

tadalafil liquid online cialis tadalafil best price [url=http://hitcialisosn.com/]buy cialis[/url]

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

Pages

Add new comment