Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make non-English chars like "å" count as "aa" when looping through text()

Tags:

jquery

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?

like image 370
Meek Avatar asked May 14 '13 12:05

Meek


2 Answers

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

like image 141
eeun Avatar answered Apr 28 '23 12:04

eeun


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

like image 33
Yeronimo Avatar answered Apr 28 '23 13:04

Yeronimo