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

hollywood casino online slots free slots games online free slot games online slots free games | https://realmoneyslots.icu/ - free slot games https://realmoneyslots.icu/ - real casino slots

foxwoods online casino three rivers casino sugarhouse casino online free casino games vegas world | https://onlinecasinox.icu/ - virgin casino online nj https://onlinecasinox.icu/ - paradise casino

slots farm online slots free casino slots free world class casino slots | https://vegasslotsonline.icu/ - heart of vegas slots https://vegasslotsonline.icu/ - free slots no download

slots farm online slots free casino slots free world class casino slots | https://vegasslotsonline.icu/ - heart of vegas slots https://vegasslotsonline.icu/ - free slots no download

casino games casinos online parx online casino maryland live casino online | https://freeonlinecasino.icu/ - gsn casino games https://freeonlinecasino.icu/ - real money casino

casino games casinos online parx online casino maryland live casino online | https://freeonlinecasino.icu/ - gsn casino games https://freeonlinecasino.icu/ - real money casino

hollywood casino online slots high 5 casino posh casino online treasure island casino minnesota | https://slotsonline.ooo/ - sandia casino https://slotsonline.ooo/ - mgm online casino

hollywood casino online slots high 5 casino posh casino online treasure island casino minnesota | https://slotsonline.ooo/ - sandia casino https://slotsonline.ooo/ - mgm online casino

hollywood casino free slots online da vinci diamonds free online slots free slots with no download or registration free online slots | https://realmoneyslots.icu/ - free slot play https://realmoneyslots.icu/ - pop slots casino

hollywood casino free slots online da vinci diamonds free online slots free slots with no download or registration free online slots | https://realmoneyslots.icu/ - free slot play https://realmoneyslots.icu/ - pop slots casino

hollywood casino online best online casinos free casino games slot machines online gambling casino | https://onlinecasinox.icu/ - hollywood online casino https://onlinecasinox.icu/ - parx online casino

hollywood casino online best online casinos free casino games slot machines online gambling casino | https://onlinecasinox.icu/ - hollywood online casino https://onlinecasinox.icu/ - parx online casino

viagra soft generic viagra names viagra generic viagra review

viagra soft generic viagra names viagra generic viagra review

posh casino doubledown casino facebook posh casino online lady luck online casino | https://freeonlinecasino.icu/ - caesars casino online https://freeonlinecasino.icu/ - red rock casino

house of fun free slots online slots freeslots.com slots free slots games | https://vegasslotsonline.icu/ - penny slots free https://vegasslotsonline.icu/ - freeslots.com slots

house of fun free slots online slots freeslots.com slots free slots games | https://vegasslotsonline.icu/ - penny slots free https://vegasslotsonline.icu/ - freeslots.com slots

free casino slot games caesars slots free casino free slots no download play free slots | [url=https://realmoneyslots.icu/]free vegas world slots[/url] [url=https://realmoneyslots.icu/]goldfish casino slots free[/url]

free casino slot games caesars slots free casino free slots no download play free slots | [url=https://realmoneyslots.icu/]free vegas world slots[/url] [url=https://realmoneyslots.icu/]goldfish casino slots free[/url]

pala casino hollywood casino play4fun free slots casino games casino games online | https://slotsonline.ooo/ - real money casino https://slotsonline.ooo/ - big fish casino slots

rock n cash casino slots bovada casino viejas casino online casino games free | https://onlinecasinox.icu/ - four winds casino https://onlinecasinox.icu/ - cafe casino online

rock n cash casino slots bovada casino viejas casino online casino games free | https://onlinecasinox.icu/ - four winds casino https://onlinecasinox.icu/ - cafe casino online

online slots real money free slots casino games free vegas slots online penny slots free online | https://vegasslotsonline.icu/ - house of fun free slots https://vegasslotsonline.icu/ - free slot play no download

turning stone casino casinos in iowa empire city online casino parx casino | https://casinoo.ooo/ - belterra casino https://casinoo.ooo/ - real money casino

double down casino free casino games slot machines casino games paradise casino | https://freeonlinecasino.icu/ - casino near me https://freeonlinecasino.icu/ - slots for real money

double down casino free casino games slot machines casino games paradise casino | https://freeonlinecasino.icu/ - casino near me https://freeonlinecasino.icu/ - slots for real money

free games for casino slots hollywood cashman casino slots quick hit slots slots of vegas casino | https://realmoneyslots.icu/ - slot games free https://realmoneyslots.icu/ - free slots no download

san manuel casino hollywood casino play4fun rivers casino pittsburgh winstar world casino | https://slotsonline.ooo/ - red rock casino https://slotsonline.ooo/ - pala casino

hard rock casino atlantic city downstream casino mgm online casino nj play free vegas casino games | https://onlinecasinox.icu/ - casino near me https://onlinecasinox.icu/ - viejas casino

hard rock casino atlantic city downstream casino mgm online casino nj play free vegas casino games | https://onlinecasinox.icu/ - casino near me https://onlinecasinox.icu/ - viejas casino

heart of vegas free slots free online casino slots free slots no download no registration turning stone online slots | https://vegasslotsonline.icu/ - vegas free slots https://vegasslotsonline.icu/ - charlestown races and slots

dakota sioux casino free online casino slots high 5 casino free casino games online | https://casinoo.ooo/ - foxwoods casino https://casinoo.ooo/ - gsn casino games

hollywood casino online slots play online casino harrah cherokee casino foxwoods casino online slots | https://freeonlinecasino.icu/ - grand falls casino https://freeonlinecasino.icu/ - online casino reviews

hollywood casino online slots play online casino harrah cherokee casino foxwoods casino online slots | https://freeonlinecasino.icu/ - grand falls casino https://freeonlinecasino.icu/ - online casino reviews

pch slots vegas world slots casino slots free wizard of oz slots | https://realmoneyslots.icu/ - free online slots vegas world https://realmoneyslots.icu/ - free online slots vegas world

pch slots vegas world slots casino slots free wizard of oz slots | https://realmoneyslots.icu/ - free online slots vegas world https://realmoneyslots.icu/ - free online slots vegas world

slot machines sugarhouse online casino caesar casino online slot games winstar casino | https://slotsonline.ooo/ - belterra casino https://slotsonline.ooo/ - free casino games no download

mohegan sun free online slots vegas world free games online slots myvegas slots jackpot magic slots | https://vegasslotsonline.icu/ - house of fun slots https://vegasslotsonline.icu/ - cleopatra slots

mohegan sun free online slots vegas world free games online slots myvegas slots jackpot magic slots | https://vegasslotsonline.icu/ - house of fun slots https://vegasslotsonline.icu/ - cleopatra slots

foxwoods online casino potawatomi casino ilani casino real money casino | https://casinoo.ooo/ - casino real money https://casinoo.ooo/ - online gambling sites

pch slots tournament house of fun free slots penny slots free online slots of vegas casino | https://realmoneyslots.icu/ - pch slots tournament https://realmoneyslots.icu/ - slotomania on facebook

free slots games online slots free games online slots free casino slot games | https://vegasslotsonline.icu/ - slots free online https://vegasslotsonline.icu/ - online casino slots no download

online gambling casino online slot machines hollywood casino play4fun mgm online casino nj | https://slotsonline.ooo/ - chumba casino https://slotsonline.ooo/ - grand falls casino

online gambling casino online slot machines hollywood casino play4fun mgm online casino nj | https://slotsonline.ooo/ - chumba casino https://slotsonline.ooo/ - grand falls casino

online casino games free four winds casino island view casino online casino games free | https://casinoo.ooo/ - vegas world casino games https://casinoo.ooo/ - casino near me

free video slots brian christopher slots slots free online free slot games with no download | https://realmoneyslots.icu/ - best online slots https://realmoneyslots.icu/ - house of fun free slots

free video slots brian christopher slots slots free online free slot games with no download | https://realmoneyslots.icu/ - best online slots https://realmoneyslots.icu/ - house of fun free slots

casino bonus online casinos sugarhouse casino prairie meadows casino | [url=https://onlinecasinox.icu/]san manuel online casino[/url] [url=https://onlinecasinox.icu/]free vegas casino games[/url]

casino bonus online casinos sugarhouse casino prairie meadows casino | [url=https://onlinecasinox.icu/]san manuel online casino[/url] [url=https://onlinecasinox.icu/]free vegas casino games[/url]

vegas world free slots slot games free free slot games with no download online slots | https://vegasslotsonline.icu/ - free slots with no download or registration https://vegasslotsonline.icu/ - hollywood casino online slots free

Pages

Add new comment