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 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

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

get loans online personal loans advanced payday loans loans with bad credit

get loans online personal loans advanced payday loans loans with bad credit

loans no bank account accredited debt relief bad credit installment loans fax free payday loan payday

loans no bank account accredited debt relief bad credit installment loans fax free payday loan payday

fast loans bad credit cash fast payday loan loans advance usa payday advance

fast loans bad credit cash fast payday loan loans advance usa payday advance

same day loans bad credit fast cash and payday loan can payday loan companies garnish your wages credit debt counseling

installment payday loans loans now auto loans people bad credit bad credit loans

installment payday loans loans now auto loans people bad credit bad credit loans

need loan 3 month payday loans guaranteed payday loans direct lenders 1500 dollar payday loans

need loan 3 month payday loans guaranteed payday loans direct lenders 1500 dollar payday loans

fast auto payday loans fast payday loans for check cash advance fast payday loans for

fast auto payday loans fast payday loans for check cash advance fast payday loans for

personal loans for people with bad credit payday loans yes fast payday loans online payday loans fax

credit card consolidation loans loan payday savings biggest cash payday loan speedy cash

credit card consolidation loans loan payday savings biggest cash payday loan speedy cash

payday loan and $1500 payday loans same day loans short term cash loans

payday loan and $1500 payday loans same day loans short term cash loans

payday advance loans long term cash loans payday loans online payday loans albuquerque

open bank account fast loans with bad credit payday loans 1 hour low interest loans

bad credit payday loans lenders 1500 dollar payday loans usa payday advance 1000 payday loan

bad credit payday loans lenders 1500 dollar payday loans usa payday advance 1000 payday loan

direct payday lenders no teletrack 24 hour payday loans bad credit no payday loans payday cash advances

direct payday lenders no teletrack 24 hour payday loans bad credit no payday loans payday cash advances

cash 2u payday loans debt consolidation home loan payday advance a payday loan company

fast cash payday loans online between payday loans inc advanced check cashing payday loans loan payoff calculator

can payday loan companies sue a payday loan lender for online payday loans affiliate payday loan

payday america free debt advice get payday loan cheap loans bad credit

payday america free debt advice get payday loan cheap loans bad credit

cheapest real cialis buy cheap cialis australia
cheap cialis 20mg pills cheap generic cialis from canada
http://atinyact.com/ cheap Female Cialis
cheap generic cialis shipped from usa

cheapest real cialis buy cheap cialis australia
cheap cialis 20mg pills cheap generic cialis from canada
http://atinyact.com/ cheap Female Cialis
cheap generic cialis shipped from usa

personal loans online best payday loan lenders cash loan need personal loan

personal loans online best payday loan lenders cash loan need personal loan

payday loans no credit check direct lender cheap personal loans best payday loan on line freedom debt relief reviews

payday loans no credit check direct lender cheap personal loans best payday loan on line freedom debt relief reviews

cheapest cialis pills cheap cialis fast
cialis for cheap cheapest place to order cialis
http://atinyact.com/#cheap cheap cialis cipa
cheap cialis drug

cheapest cialis pills cheap cialis fast
cialis for cheap cheapest place to order cialis
http://atinyact.com/#cheap cheap cialis cipa
cheap cialis drug

payday loans 1 hour freedom debt relief reviews federal student loans best payday loan website

payday loans 1 hour freedom debt relief reviews federal student loans best payday loan website

paydayloansinchicago online personal loans bad credit instant loans instant personal loan

paydayloansinchicago online personal loans bad credit instant loans instant personal loan

best payday advance loans fast payday loans no fax personal loans with bad credit consolidate private student loans

best payday advance loans fast payday loans no fax personal loans with bad credit consolidate private student loans

internet payday loans 10 dollar payday loan payday loans online apply loan bad credit

1 hour payday loans no faxing payday loans texas low interest loan auto loan bad credit

1 hour payday loans no faxing payday loans texas low interest loan auto loan bad credit

cheap cialis100mg cheap cialis
cheap cialis soft buy cialis online cheap prices
http://atinyact.com/#cialis get cialis cheap
cheapest cialis australia

cheap cialis100mg cheap cialis
cheap cialis soft buy cialis online cheap prices
http://atinyact.com/#cialis get cialis cheap
cheapest cialis australia

Pages

Add new comment