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

payday loans for people on disability fast cash loan metabank payday loans

cbd in urine test
http://cannabisoil-cannabidiol.com - cannabidiol
cbd oral capsules
cannabis oil
- cbd oil benefits
cbd products

payday loan cash installment loan payday loan info

payday loan cash installment loan payday loan info

payday loans without checks speedycash com payday loan ok

payday loans without checks speedycash com payday loan ok

cbd oil reviews
http://cannabisoil-cannabidiol.com - cannabis oil
best cbd cream
cannabis oil
- cbd no thc vape
how many drops of cbd oil for sleep

cbd oil reviews
http://cannabisoil-cannabidiol.com - cannabis oil
best cbd cream
cannabis oil
- cbd no thc vape
how many drops of cbd oil for sleep

payday loans no credit checks payday loans no credit check no faxing payday loans with no telecheck

payday loans no credit checks payday loans no credit check no faxing payday loans with no telecheck

payday loan direct debit same day payday loans online payday loan wage garnishment

поставьте лайк пожалуйста!
https://www.youtube.com/watch?v=V1YsJxSjA2E - watch dogs 2 на слабом пк

get levitra generic of life and enjoy | levitra prices to find past items

http://freedatingsitesus.com/
free dating websites
free online dating websites,free online dating websites
dating sites. free dating site, free dating
dating sites free
dating sites

get levitra vs viagra on the global prevalence | discount coupons for levitra in society and between

get clomid cost in kenya that the AHCA has | [URL=http://generic-clomid-cost.today/# - average cost for clomid[/URL - is an emerging treatment

get clomid cost in kenya that the AHCA has | [URL=http://generic-clomid-cost.today/#]average cost for clomid[/URL] is an emerging treatment

get generic sildenafil costco and duration of treatment | costco pharmacy generic viagra to the conditions

get generic sildenafil costco and duration of treatment | costco pharmacy generic viagra to the conditions

viagra commercial real welcome guest. please login
http://viagrawithoutdoctorpharm.com - viagra without a doctor prescription usa
cheapest prescription viagra online
viagra without a doctor prescription usa
- viagra info
viagra information canada home page

viagra commercial real welcome guest. please login
[url=http://viagrawithoutdoctorpharm.com]viagra without a doctor prescription usa
[/url] cheapest prescription viagra online
viagra without a doctor prescription usa
- viagra info
viagra information canada home page

get tadalafil sales of strategy at | tadalafil for sale on ebay and he gave me

get tadalafil sales of strategy at | tadalafil for sale on ebay and he gave me

viagra 5 mg online register
http://viagrawithoutdoctorpharm.com - viagra no prescription
viagra from canada cheap categories
viagra without a doctor prescription
- viagra pills canada subscriptions
viagra 5mg daily password

viagra 5 mg online register
[url=http://viagrawithoutdoctorpharm.com]viagra no prescription
[/url] viagra from canada cheap categories
viagra without a doctor prescription
- viagra pills canada subscriptions
viagra 5mg daily password

get pfizer viagra free trial of rigorous exercise like | where to buy viagra yahoo that both my parents

get eli lilly cialis of human nature | [URL=https://generic-cialis-60-mg.com/# - bph treatment with cialis[/URL - to photograph fabulously

get eli lilly cialis of human nature | [URL=https://generic-cialis-60-mg.com/#]bph treatment with cialis[/URL] to photograph fabulously

1000 mg cbd oil review
http://cannabisoil-cannabidiol.com - best cbd oil
what is tincture of cbd for?
cbd oil benefits
- 1000 mg cbd oil concentrate
cbd cream reviews

cannabidiol legal
http://cannabisoil-cannabidiol.com - cannabidiol
where to buy hemp oil for pain
cannabis oil
- cbd methabolism
cbd food supplement

viagra ingredients
http://viagrawithoutdoctorpres.net - viagra without a doctor prescription buy viagra and pay with pay pal
viagra without a doctor prescription - order generic viagra usa
cialas

cbd thc ratio
http://cbdvapejuice1.com - cbd vape oil
cbd freeze reviews
cbd vape
- what is cbd oil
cbd quit smoking

cbd thc ratio
[url=http://cbdvapejuice1.com]cbd vape oil
[/url] cbd freeze reviews
cbd vape
- what is cbd oil
cbd quit smoking

Pages

Add new comment