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

viagra dosage is there generic viagra natural viagra alternative cheapest generic viagra

viagra patent viagra online canadian pharmacy buy female viagra where to buy viagra online

viagra patent viagra online canadian pharmacy buy female viagra where to buy viagra online

viagra soft tablets order viagra online cheapest generic viagra where can i buy viagra online

viagra soft tablets order viagra online cheapest generic viagra where can i buy viagra online

viagra without prescription free viagra samples online viagra purchase viagra buy online

viagra prices when will viagra be generic buy viagra no prescription viagra online canada

cheap generic viagra when will generic viagra be available generic brands of viagra online viagra generic

cheap generic viagra when will generic viagra be available generic brands of viagra online viagra generic

buy viagra soft tabs generic viagra online pharmacy viagra on line best place to buy viagra online

buy viagra soft tabs generic viagra online pharmacy viagra on line best place to buy viagra online

purchase viagra buy viagra online online generic viagra generic name for viagra

purchase viagra buy viagra online online generic viagra generic name for viagra

generic viagra cheap viagra online no prior prescription cheap viagra pills generic viagra reviews

generic viagra cheap viagra online no prior prescription cheap viagra pills generic viagra reviews

buy cheap cialis pills cheap reliable cialis
cheap reliable cialis find cialis cheap
http://atinyact.com/#buy-cialis cialis sale cheap
cheapest cialis generic

payday loans online payday online loan advanced check cashing payday loans day payday loans online

quick easy personal loans no fax same day payday loans online long term payday loans quick loans online

buy brand cialis cheap cheapest cialis super active
http://atinyact.com/#cialis cheap cialis super activemailorder.com
http://atinyact.com/#cialis cheap cialis generic canada
cheapest online pharmacy for cialis

buy brand cialis cheap cheapest cialis super active
http://atinyact.com/#cialis cheap cialis super activemailorder.com
http://atinyact.com/#cialis cheap cialis generic canada
cheapest online pharmacy for cialis

online loan fast payday loans no fax no fax same day payday loans long term personal loans

payday loan no credit check personal loan online anyday payday loans unsecured loans for bad credit

payday loan no credit check personal loan online anyday payday loans unsecured loans for bad credit

cialis generika cheap cialis pills for cheap
cialis for slae cheap cheap cialis pills
http://atinyact.com/ cheap cialis in the united states
cheap cialis online pharmacy

no credit check fast auto payday loan fast cash loan no faxing 1 hour payday loan

payday loan online online payday loan unsecured personal loan paydays loans online

az laws payday loans instant loan loan consolidation payday finance

az laws payday loans instant loan loan consolidation payday finance

card consolidate credit debt fast cash payday loan emergency loan quick loan personal loans with bad credit easy approval personal loans

card consolidate credit debt fast cash payday loan emergency loan quick loan personal loans with bad credit easy approval personal loans

can a payday loan garnish my wages cash loans loans online fast approval no fax payday loan today

payday america loan payday online best payday loan service payday online loan

payday america loan payday online best payday loan service payday online loan

http://atinyact.com/#cialis canada cialis cheap no prescription
purchase cheap cialis cheapest brand cialis
http://atinyact.com/#cialis-cheap cheapest price for cialis
buy cialis online cheapest

bad credit personal loans fax free payday loan payday loan payday loans cash advances fast payday loan no credit

bad credit personal loans fax free payday loan payday loan payday loans cash advances fast payday loan no credit

payday loans with bad credit net cash money loan fast payday loan in

payday loans with bad credit net cash money loan fast payday loan in

consolidate debt loans fast payday loans of absolutely no fax payday loan payday loans in texas

no credit check loan cash loan shop federal student loans online loan shop com

no credit check loan cash loan shop federal student loans online loan shop com

bad credit payday loans online bad credit instant loans a payday loan no faxing fax free payday loan payday

badcredit payday loans payday loans online auto loans online payday loans

direct loans lead loans bad credit instant payday loans payday loan

direct loans lead loans bad credit instant payday loans payday loan

no credit check mortgage payday loans online personal loans poor credit online payday payday loan

no credit check mortgage payday loans online personal loans poor credit online payday payday loan

payday loans no credit check get out credit card debt quick loans bad credit private student loan consolidation

payday loans no credit check get out credit card debt quick loans bad credit private student loan consolidation

buy sale cheap cialis cheap cialis soft tabs
cheap cialis melbourne buy cialis cheap prices fast delivery
http://atinyact.com/#cialis cheap cialis tablets uk
cheapest cialis pills

Pages

Add new comment