Highlight multiple keywords for jQuery.autocomplete
Asked Answered
O

3

5

I'm using the jQuery Autocomplete plugin, but I'm having some problems with the result highlighting. When a match is found, but the entered keyword contains spaces, there's no highlighting. Example:

search = "foo", result = "foo bar", displayed = "foo bar"

search = "foo ba", result = "foo bar", displayed = "foo bar"

So, I'm trying to fix this using the highlight option of the autocomplete function where you can use a function to do some custom stuff with the results. Currently I have this:

$('.autocomplete').autocomplete('getmatch.php', {
    highlight: function(match, keywords) {
        keywords = keywords.split(' ').join('|');
        return match.replace(/(get|keywords|here)/gi,'<b>$1</b>');
    }
});

The replace function replaces all the matched words in the string with a bold version, that works. However, I don't know how to get the keywords into that function. I though I'd split them, and then join them with a '|', so "foo bar" ends up like "foo|bar". But something like this doesn't seem to work:

return match.replace(/(keywords)/gi,'<b>$1</b>'); // seen as text, I think

return match.replace('/'+(keywords)+'/gi','<b>$1</b>'); // nothing either

Any ideas?

Outgrow answered 5/6, 2009 at 17:52 Comment(0)
L
11

Use the RegExp constructor to create a RegExp object from a string:

$('.autocomplete').autocomplete('getmatch.php', {
    highlight: function(match, keywords) {
        keywords = keywords.split(' ').join('|');
        return match.replace(new RegExp("("+keywords+")", "gi"),'<b>$1</b>');
    }
});
Lachrymose answered 5/6, 2009 at 17:56 Comment(4)
Thanks, this works for the replacement of all the keywords! However, they are replaced with "<b>$1</b>" — the actual text $1, and not the keyword itself that $1 should represent?Outgrow
Fixed it by grouping the keywords.Lachrymose
the purpose of the highlighting is so that if the value you typed matches an item, but isn't the item, the user can replace it easily. Think of how in Excel, when you type "Ma" and the cells above it say "Male" - it will autofill the "le" but leave them highlighted, so that you can continue to type "nifest" without stopping.Beyond
Its working fine for english keywords, But how to do for indian language specially hindi language?Simony
Q
1

I tweaked the initial autocomplete implementation as the above is simplified and won't deal with regEx special characters in the term.

function doTheHighlight(value, term) {
    // Escape any regexy type characters so they don't bugger up the other reg ex
    term = term.replace(/([\^\$\(\)\[\]\{\}\*\.\+\?\|\\])/gi, "\\$1");

    // Join the terms with a pipe as an 'OR'
    term = term.split(' ').join('|');

    return value.replace(new RegExp("(?![^&;]+;)(?!<[^<>]*)(" + term + ")(?![^<>]*>)(?![^&;]+;)", "gi"), "<strong>$1</strong>");
}
Quaternary answered 28/9, 2009 at 14:19 Comment(0)
M
0

Your function should use this signature: function(value, term). The value & term will be populated by the autocomplete plug-in and have the values you need.

Millinery answered 5/6, 2009 at 18:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.