I have a script which searches dynamically through a html table. See example here. It works fine, but I would like a <tr>
with the letter "å" to appear if the user enters "aa" in the search field. And vice versa. ('å' is a equivalent to 'aa' and 'aa' is a equivalent to 'å').
I would like to avoid hidden text in the markup, and instead handle the alternative spelling in the script. I guess it could be done by creating some sort of special character map - I just have no idea how to do this.
Anyone got any ideas?
What you need to do is to replace your regular expression /aa/ with /((å)|(aa))/.
Here's some code that will handle the replacements for you, it's creating a regular expression to generate the regular expression...
var replace = [['å','aa'], ['ß','ss']];
for (var i=0;i<replace.length;i++){
var r = replace[i];
var reg = new RegExp('(('+r[0]+')|('+r[1]+'))');
inputVal = inputVal.replace(reg, '(('+r[0]+')|('+r[1]+'))');
}
Or see it in action here... http://tinker.io/b04e6/9
I updated your code. You could try something like this:
var val = $(this).val().replace(/\w\w/g, function(c) {
return {
"AA" : "AA|Å",
"aa" : "aa|å"
}[c] || c;
});
Updates to regex to replace with aa or å
See updated tinker: http://tinker.io/b04e6/13
Uses a replace function. You can map your desired characters in the function
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With