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

pharmacy viagra best generic viagra cheap viagra uk is there a generic viagra

pharmacy viagra best generic viagra cheap viagra uk is there a generic viagra

buy generic viagra viagra online viagra from india generic for viagra

buy generic viagra viagra online viagra from india generic for viagra

viagra online online viagra viagra generic cheap viagra online

viagra online online viagra viagra generic cheap viagra online

viagra information viagra buy online buy female viagra where to buy viagra online

viagra information viagra buy online buy female viagra where to buy viagra online

cialis nz
cheap cialis 20mg what is herbal viagra
cheap generic cialis tadalafil viagra gen
viagra generic online
cheap cialis professional online where to buy legal viagra
cheaper version of cialis http://katsnethen.com

cialis nz
cheap cialis 20mg what is herbal viagra
cheap generic cialis tadalafil viagra gen
viagra generic online
cheap cialis professional online where to buy legal viagra
cheaper version of cialis http://katsnethen.com

can i go to canada to buy viagra
cheap cialis free shipping cialis online, overnight delivery
cheap cialis super active parent canada viagra
female viagra does it work
cheapest cialis online online viagra sale
cialis for cheap http://ticklesaurus.net

can i go to canada to buy viagra
cheap cialis free shipping cialis online, overnight delivery
cheap cialis super active parent canada viagra
female viagra does it work
cheapest cialis online online viagra sale
cialis for cheap http://ticklesaurus.net

natural alternative to viagra viagra online no prior prescription how to use viagra online viagra

real viagra for sale generic viagra review generic viagra india generic viagra online

viagra brand
cheapest cialis on the internet purchase viagra
cialis 500 pills for cheap price phone number to buy viagra
buying cheap cialis online
cheap cialis here cialis and paypal forum
cialis sublingual cheap http://samrichards.info

cheap viagra when will viagra be generic buy female viagra cheap generic viagra

webseite
cheap cialis super active parent buy viagra in florida
cheap cialis100mg what countries can you buy viagra over the counter
buy cialis online credit card
buy cialis for cheap from us pharmacy buy 5mg daily cialis cheap
cheap brand cialis online http://gbplacing.com

purchase viagra buy viagra online canada viagra price online viagra reviews

purchase viagra buy viagra online canada viagra price online viagra reviews

is it illegal to buy viagra from craigslist
cialis 500 pills for cheap price next day cialis delivery
cheap genuine cialis uk generic cialis paypal
cialis prix de la boite
cheapest chemist for cialis viagra online buy india
cheapest pharmacy to buy cialis http://worldinconversation.info

is it illegal to buy viagra from craigslist
cialis 500 pills for cheap price next day cialis delivery
cheap genuine cialis uk generic cialis paypal
cialis prix de la boite
cheapest chemist for cialis viagra online buy india
cheapest pharmacy to buy cialis http://worldinconversation.info

prescription viagra is there generic viagra discount generic viagra online pharmacy viagra

prescription viagra is there generic viagra discount generic viagra online pharmacy viagra

generic viagra online pharmacy viagra generic female viagra pills viagra online no prior prescription

generic viagra online pharmacy viagra generic female viagra pills viagra online no prior prescription

buy viagra canada viagra online cheap order viagra online online viagra reviews

mail order viagra viagra online generic viagra online generic viagra

mail order viagra viagra online generic viagra online generic viagra

buy viagra real viagra online generic viagra cheap viagra prescription online

cialis buy from canada
cheap cialis here buy cialis with no perscription
cheapest cialis price buy viagra cialis online uk
can you buy viagra per pill
cheap reliable cialis 100 mg generic cialis
cheap 10mg cialis without a prescription http://theticklesaurus.com

car insurance il You can find any type of kind of medication that you might ever before require in a Canadian pharmacy. There are a number of means to do this. One is to head to Canada but an alternative is to choose a Canadian drug store online. If you do either of the abovementioned you can conveniently take a physician's prescription along with you or discover a drug store which has a clinical physician that can suggest the medications that you need. Car Insurance car and auto insurance

liquid cialis for sale
cheap retinal specialist oklahoma cheapest cialis buy
buy cheap cialis site cialis 20 mg walmart
where can i buy viagra in india
who has the cheapest cialis non pre ion viagra
cheapest cialis dosage 20mg price http://katmercer.com

free viagra sample pack how to get viagra online buy viagra professional canadian online pharmacy viagra

free viagra sample pack how to get viagra online buy viagra professional canadian online pharmacy viagra

where to buy viagra online best place to buy generic viagra online soft tabs viagra generic viagra canada

where to buy viagra online best place to buy generic viagra online soft tabs viagra generic viagra canada

car insurance pa You can find any kind of sort of drug that you might ever need in a Canadian pharmacy. There are a number of methods to do this. One is to visit Canada yet an option is to seek out a Canadian pharmacy online. If you do either of the aforementioned you can quickly take a medical professional's prescription in addition to you or discover a pharmacy which has a clinical physician that can advise the medicines that you need. what is auto insurance auto insurance comparison quotes

viagra buy is there a generic for viagra viagra brands canadian generic viagra

Pages

Add new comment