+3  A: 

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>');
    }
});
Gumbo
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?
Alec
Fixed it by grouping the keywords.
Gumbo
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.
Josh E
A: 

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.

Zachary
+1  A: 

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>");
}
Shaun