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

loans apply for payday loan money loan personal loans debt consolidation

loans bad credit payday advance loan apple payday loans 100 no fax payday loan

loans bad credit payday advance loan apple payday loans 100 no fax payday loan

viagra for women
buy cheap cialis no prescription is there a generic for cialis
cheaper alternatives to cialis cialis quotidien meilleur prix
is it legal to buy viagra from canadian pharmacy
cheap forzest cialis buy viagra online in
cheapest cialis daily http://wizardsoffear.org

bad credit payday loan lenders capital finance one payday loans payday loans no faxing no credit checks check cash advance

canadian pharmacy cialis daily use
buying cheap cialis online cialis pharmacy prices
cheapest brand cialis 100 mg cialis
where can i buy cialis online safely 56
cheap cialis next day shipping buy viagra ebay
order cheap cialis http://katmercer.com

cialis super force online shopping
buy cheap generic cialis online discount 10 mg cialis
cheapest canadian generic cialis paypal to buy viagra
where to buy viagra in philippines
cheap cialis soft tabs cialis brand overnight
cheap generic female cialis http://nicolebcleveland.com

quick personal loans bad credit online payday advance loans bad credit payday loans uk online payday loans direct lenders

quick personal loans bad credit online payday advance loans bad credit payday loans uk online payday loans direct lenders

payday loans 1 hour fax no teletrack payday loans apply credit card bad credit payday loans texas

payday loans 1 hour fax no teletrack payday loans apply credit card bad credit payday loans texas

payday loans no credit check no faxing actual payday loan companies loans consolidation loan

loans no bank account a quick payday loan bad credit personal installment loan account now payday loan

loans no bank account a quick payday loan bad credit personal installment loan account now payday loan

bad credit lender no payday loan same day payday loans direct lenders bad credit personal loans a faxless payday loan

bad credit lender no payday loan same day payday loans direct lenders bad credit personal loans a faxless payday loan

cash advance america a faxless payday loan direct payday loans affiliate payday loan

cash advance america a faxless payday loan direct payday loans affiliate payday loan

cash advances a payday loan loans payday loan business

fast payday loan 1 hour payday loan lenders loans bad credit 1 hr payday loans

unsecured loans fax instant payday loan personal loan payday online cash advance

unsecured loans fax instant payday loan personal loan payday online cash advance

cash loans payday loans americash payday loans apply for payday loan online loans now bad credit

cash loans payday loans americash payday loans apply for payday loan online loans now bad credit

applying for payday loans online payday loans direct lenders payday loans no credit check no faxing loan online payday

payday loan in fax free payday loan no teletrack payday fast payday loans loan online payday

payday loan in fax free payday loan no teletrack payday fast payday loans loan online payday

cash instant loan personal loan online bad credit absolutely no faxing payday loan fax instant payday loan

cash instant loan personal loan online bad credit absolutely no faxing payday loan fax instant payday loan

instant cash 100 payday loan and fast payday loans help debt consolidation loan

generic cialis for sale
cheapest canadian generic cialis viagra kaufen
what is cheaper than cialis buy viagra canada
overnight pharmacy 4u cialis
buy cheapest 100mg generic cialis best place to buy viagra in canada
cheapest way to get cialis http://nicolebcleveland.com

generic cialis for sale
cheapest canadian generic cialis viagra kaufen
what is cheaper than cialis buy viagra canada
overnight pharmacy 4u cialis
buy cheapest 100mg generic cialis best place to buy viagra in canada
cheapest way to get cialis http://nicolebcleveland.com

loans people bad credit 1000 easy payday loan buy payday loan leads apply loan bad credit

loans people bad credit 1000 easy payday loan buy payday loan leads apply loan bad credit

generic viagra without a doctor prescri
cheapest cialis in the world usa overnight pharmacy cialis
what is cheapest place to buy cialis cialis canadian no presciption
buy viagra 50 mg
cheapest price for cialis cialis 50 mg
cheap generic cialis from canada http://youcantsaythat.org

generic viagra without a doctor prescri
cheapest cialis in the world usa overnight pharmacy cialis
what is cheapest place to buy cialis cialis canadian no presciption
buy viagra 50 mg
cheapest price for cialis cialis 50 mg
cheap generic cialis from canada http://youcantsaythat.org

costco price for cialis 5mg
cheapest cialis super active cialis auckland
find cheap cialis without prescription can you buy viagra without prescription
mens health-cialisis
fast and cheap cialis cvs-cialis-cost
cheapest cialis in sydney http://mindsparking.org

viagra tablet
order cheap cialis ordercialis
cheaper cialis best generic viagra
cheapest place to buy viagra online uk
cheap cialis ireland viagra pills buy
cheap cialis extra strength http://thescarbehindme.com

viagra tablet
order cheap cialis ordercialis
cheaper cialis best generic viagra
cheapest place to buy viagra online uk
cheap cialis ireland viagra pills buy
cheap cialis extra strength http://thescarbehindme.com

cost of cialis at walgreens pharmacy
cheapest cialis 20mg online need to order cialis
get cialis very cheap where to buy viagra in melbourne over the counter
comprar cialis en la india
cheap cialis quick shipment us cialis packaging
cheap cialis overnight no prescription http://thescarbehindme.com

cost of cialis at walgreens pharmacy
cheapest cialis 20mg online need to order cialis
get cialis very cheap where to buy viagra in melbourne over the counter
comprar cialis en la india
cheap cialis quick shipment us cialis packaging
cheap cialis overnight no prescription http://thescarbehindme.com

buy cialis 40 mg online
cheap cialis online canadian pharmacy best place to buy generic viagra from india
cheapest generic super active cialis cialis buy sweden
prix du cialis 20mg en pharmacie
comments cialis cheapest price buy viagra soft tabs
buy cheap cialis australia http://mindsparking.com

female viagra pills best place to buy generic viagra online viagra price generic for viagra

female viagra pills best place to buy generic viagra online viagra price generic for viagra

buy viagra pills buy generic viagra viagra wholesale when will generic viagra be available

buy viagra pills buy generic viagra viagra wholesale when will generic viagra be available

Pages

Add new comment