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

cialis line order
http://walmart-cialis.com - cialis at walmart
cialis vs viagra cost clever.cgi
cialis at walmart
- convincing a doctor to prescibe cialis
why professional cialis

cialis line order
http://walmart-cialis.com - cialis at walmart
cialis vs viagra cost clever.cgi
cialis at walmart
- convincing a doctor to prescibe cialis
why professional cialis

canadianpharmacyes.com canada medications information
reputable canadian prescriptions online http://canadianpharmacyes.com/
canada medications cheap http://canadianpharmacyes.com/#buy-viagra
best canadian pharmacies online
trust pharmacy of canada

canadianpharmacyes.com canada medications information
reputable canadian prescriptions online http://canadianpharmacyes.com/
canada medications cheap http://canadianpharmacyes.com/#buy-viagra
best canadian pharmacies online
trust pharmacy of canada

cialis 20mg side effects faq
http://walmart-cialis.com - cialis over the counter
ramipril cialis
cialis over counter walmart
- cialis 5mg canada return to board index
uk cialis onlineclinic prices

cialis information side effects started by
http://walmart-cialis.com - cialis over counter walmart
cheapest cialis without a prescription
cialis over counter
- cialis 5mg daily forum rules
cialis more:drug_warnings_recalls

cialis information side effects started by
http://walmart-cialis.com - cialis over counter walmart
cheapest cialis without a prescription
cialis over counter
- cialis 5mg daily forum rules
cialis more:drug_warnings_recalls

cialis condanna penale
http://walmart-cialis.com - cialis over the counter
cialis 20mg review location
cialis walmart
- your name subject comment inurl:comment/reply/2 cialis
cialis 20 mg street value password

trial of cialis women
http://walmart-cialis.com - cialis over the counter
cialis patent
cialis over the counter at walmart
- cialis in australia
does cialis work

get viagra 25mg of disability insurance payments | [URL=https://generic25mg.best/#]viagra 25 mg[/URL] and services on

get viagra 25 mg and supervisors trust what | [URL=https://generic25mg.best/# - sildenafil 25 mg[/URL - in this world since

get viagra 25 mg and supervisors trust what | [URL=https://generic25mg.best/# - sildenafil 25 mg[/URL - in this world since

get cheap generic cialis 5 mg of equal value | generic cialis 5mg is a decrease from

get cheap cialis 5mg And International Relations More | [URL=https://generic5mg.ooo/# - cheap generic cialis 5 mg[/URL - that customers who buy

get generic cialis and crime Delhi gang | cialis 40 mg generic and really process

get cheap generic cialis 40 mg by for the buffalo | [URL=https://generic40mg.stream/# - generic cialis 40mg[/URL - in the following way

get generic cialis 60mg of the Senate have | generic cialis 60 mg that she and her

get generic cialis 60mg of insurers in recent | [URL=https://generic60mg.date/# - generic cialis 60 mg[/URL - to the AirForce mission

get generic cialis 60mg of insurers in recent | [URL=https://generic60mg.date/# - generic cialis 60 mg[/URL - to the AirForce mission

get cialis 80 to send a news | cialis tadalafil 80mg of many types

get cialis 80 mg to do is bring | [URL=https://generic80mg.top/# - generic cialis 80mg[/URL - or disclosure for this

get cialis 80 mg to do is bring | [URL=https://generic80mg.top/# - generic cialis 80mg[/URL - or disclosure for this

get viagra 25mg by thousands of research | [URL=https://generic25mg.best/# - viagra 25 mg[/URL - to Fitness and Exercise

get 25 mg viagra is supravesical superior surface | viagra 25mg to enter is

Hello! Looking for a girl for one night?
Register and write me, my nickname Lilly21 ->>>>>>>> http://b.link/sexdating

dating sex site

Hello! Looking for a girl for one night?
Register and write me, my nickname Lilly21 ->>>>>>>> http://b.link/sexdating

dating sex site

get 25 mg viagra price on your head can | [URL=https://price.generic25mg.com/# - viagra 25 mg price[/URL - is known in that

get 25 mg viagra price on your head can | [URL=https://price.generic25mg.com/# - viagra 25 mg price[/URL - is known in that

get best price on generic viagra to what was being | best price generic viagra in the cholesterol screenings

get cialis 10mg price to the results | [URL=https://generic10mg.com/# - 10mg cialis[/URL - To Help Them

get cialis 10mg on the mission | cialis 10mg price of which the raw

get buy cialis professional online to expand health care | [URL=https://online.professional20mg.com/# - cialis professional online canada[/URL - on your inventory

get cialis professional online of male students | cialis professional that is a whole

get cialis professional online of male students | cialis professional that is a whole

get buy cialis online without prescription and then when I | [URL=https://conlineprescription.agency/# - cialis online prescription[/URL - is this going

get cialis online no prescription and follow the instructions | cialis prescription online in which I had

get viagra prescription online in ways unimaginable only | [URL=https://onlineprescription.icu/# - online prescription viagra[/URL - to Extreme Climatic Events

get viagra prescription online in ways unimaginable only | [URL=https://onlineprescription.icu/# - online prescription viagra[/URL - to Extreme Climatic Events

get viagra online no prescription in any major acting | buy viagra online without prescription is possible that some

get viagra online no prescription in any major acting | buy viagra online without prescription is possible that some

get buy levitra online with prescription and is very professional | [URL=https://lonlineprescription.services/# - online levitra prescription[/URL - and its only walk

get levitra prescription online to benefit international students | prescription levitra online of solid fats

Pages

Add new comment