I need to compare 2 strings as equal such as these:
Lubeck == Lübeck
In JavaScript.
Why? Well, I have an auto-completion field that's going out to a Java service using Lucene, where place names are stored naturally (as Lübeck), but also indexed as normalized text,
import sun.text.Normalizer;
oDoc.setNameLC = Normalizer.normalize(oLocName, Normalizer.DECOMP, 0)
.toLowerCase().replaceAll("[^\p{ASCII}]","");
This way some-one who doesn't know to type "Mèxico" can type "mexico" and get a match which returns "Mèxico" (among a lot of other possible hits, like "Café Mèxico, Dubai, UAE").
Now the thing is I don't have the ability to change the service to do any highlighting on the server side, therefore I am highlighting on the client JavaScript side with something like:
return result.replace( input.replace(/[aeiou]/g,"."), "<b>$1</b>");
It's a little more fancy because I am escaping special regex characters in the input. This is fine for simple one word matches at the beginning of a hit, but it really breaks down if you suddenly wish to support multi-word matches like "london cafe":
input = input.strip().toLowerCase(); //fyi prototype's strip is like trim
re = new RegEx(input.replace(/[aeiou]/g,".").replace(/s+/g,"|"),"gi");
return result.replace(re, "<b>$1</b>");
This doesn't work for say "london ca" (was typing london cafe), because it would mark "Jack London Cabin, Dawson City, Canada" as: "Ja<b>ck</b> <b>London</b> <b>ca</b>bin, Dawson <b>Ci</b>ty, <b>Ca<b/>nada"
[note the "ck" and "Ci" particularly]
Therefore I'm sort of looking for something that's not as crazy as:
input = input.strip().toLowerCase();
input = input.replace(/a/g,"[ààáá?a????????āā????]");
input = input.replace(/e/g,"[èèééêê??ēē??????ěě]");
// ditto for i, o, u, y, c, n, maybe also d, g, h, j, k, l, r, s, t, w, z
re = new RegEx(input.replace(/s+/g,"|"),"gi");
return result.replace(re, "<b>$1</b>");
Is there a compiled table I can refer to mapping a range of characters which are accented versions of an other character to that character, by which I don't mean the plain unicode chart. And if so, could I avoid using weird, possibly slow, RegEx statements?
About the bounty:
Before I started a bounty there were two answers, the one pointing me to doing it in Ruby, and the one that MizzardX wrote which was a completion of the basic form I'd put in my question. Now don't get me wrong, I really appreciate working it out as completely as he did, but I just wished that there might be another way. It seems so far that everyone who's dropped by to look at the question and answer has decided that MizzardX covers it just fine, or that they have no different approach. I would be interested in a different approach, and if it simply isn't available before the bounty closes, MizzardX will win the bounty (though in a cruel twist, his edits mad it a community wiki answer, so I'm not sure if he'll get the bounty!)
See Question&Answers more detail:
os