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

cash advance near me payday loans no credit check online small personal loans online 1 hour faxless payday loan

cash loans payday loans 10 dollar payday loan online payday loans 1000 easy payday loan

approved payday loans 1 hour payday loans best payday loan 1 hour payday loans

fast payday loan online advanced payday loan can payday loans garnish your wages direct lender of payday loans

loans without credit checks moneytree payday loans quick bad credit loan all payday loan companies

loans without credit checks moneytree payday loans quick bad credit loan all payday loan companies

tax debt problems affordable payday loan online advance payday loans bank personal loan

tax debt problems affordable payday loan online advance payday loans bank personal loan

online payday loans fast debt reduction services finance payday quick cash loan

debt consolidation companies fax payday loan faxless payday loan payday consolidation loans fax payday loan funds

debt consolidation companies fax payday loan faxless payday loan payday consolidation loans fax payday loan funds

1500 dollar payday loans approved payday loans guaranteed bad credit personal loans car title and payday loan

1500 dollar payday loans approved payday loans guaranteed bad credit personal loans car title and payday loan

a payday loan bad credit payday loans in instant online payday loan 1 hour payday loans

apple payday loans bank personal loan debt counseling loan small business

apple payday loans bank personal loan debt counseling loan small business

credit debt management 1 hr payday loans loans for poor credit 100 guaranteed payday loans

credit debt management 1 hr payday loans loans for poor credit 100 guaranteed payday loans

payday day loans fax payday loan faxless payday loan payday loans small business online payday advance loans

payday day loans fax payday loan faxless payday loan payday loans small business online payday advance loans

bad credit same day payday loan fast payday loans of advance payday loans check advance

cash advance payday loan fax no teletrack payday loans payday loan direct lender consolidate private student loans

cash advance payday loan fax no teletrack payday loans payday loan direct lender consolidate private student loans

fast payday loan online card consolidate credit debt fast cash personal loans 1 hour faxless payday loan

car loans for bad credit low cost payday loans and fast payday loans cheap loans bad credit

installment loans no credit check check cash consolidated credit get fast cash

fanda pharmacy hong kong cialis
buy cheap brand cialis online generic viagra without a doctor preion
cheap cialis super activerideshop.com buy cialis online
where can you buy viagra cheap
cheap cialis ireland legal buy viagra online canada
cheap cialis with free shipping http://concretepumpexpert.com

fanda pharmacy hong kong cialis
buy cheap brand cialis online generic viagra without a doctor preion
cheap cialis super activerideshop.com buy cialis online
where can you buy viagra cheap
cheap cialis ireland legal buy viagra online canada
cheap cialis with free shipping http://concretepumpexpert.com

bad credit debt consolidation internet payday loan 1500 payday loan a payday loan lender debt consolidation help

payday loan and plaingreenloans com cash loans for bad credit car title & payday loan

payday loan and plaingreenloans com cash loans for bad credit car title & payday loan

instant cash personal loan direct lender of payday loans payday loans no credit check online military loans

bank personal loan direct lender of payday loans online installment loans all online payday loans

purchase viagra professional
cheap cialis sydney viagra cheapest price
find cheap cialis without prescription low priced cialis
cialis and priligy
what pharmacy has the cheapest cialis viagra spray buy online
get cialis very cheap http://worldinconversation.info

purchase viagra professional
cheap cialis sydney viagra cheapest price
find cheap cialis without prescription low priced cialis
cialis and priligy
what pharmacy has the cheapest cialis viagra spray buy online
get cialis very cheap http://worldinconversation.info

cialis ed pack paypal
cheap non-generic cialis can i buy viagra over the counter in the usa
best cheap cialis online cialis 5 mg daily success
brand name cialis cheap 21
super cialis cheap cialis on line buy
cheap cialis 20mg tablets in australia http://centerforpublicdiplomacy.org

personal loans bad credit 24 hour payday loans online fast cash personal loans [url=https://wwwonlinepaydayloans.com/]200 cash payday loan[/url]

fast cash personal loans payday cash loan card consolidate credit debt home loans for bad credit

payday loans bad credit payday advance loans usa payday advance credit counselling

payday loans bad credit payday advance loans usa payday advance credit counselling

online loan debt help credit debt management big payday loan company

web pharmacy rx cialis
brand name cialis cheap buy brand cialis no prescription
buying cialis cheap cialis at walmart
chcialisnrx.com
dirt cheap cialis cialis sicuro online
cheap cialis without prescription http://thescarbehindme.com

web pharmacy rx cialis
brand name cialis cheap buy brand cialis no prescription
buying cialis cheap cialis at walmart
chcialisnrx.com
dirt cheap cialis cialis sicuro online
cheap cialis without prescription http://thescarbehindme.com

canadian payday loans online 400 payday loan direct loans 500 cash payday loan

canadian payday loans online 400 payday loan direct loans 500 cash payday loan

can payday loans garnish your wages fax instant payday loans apple payday loans cash advance lenders

can payday loans garnish your wages fax instant payday loans apple payday loans cash advance lenders

home loans bad credit 1500 payday loans average interest rate on a payday loan 1st payday loan free

Pages

Add new comment