loopj/jquery-tokeninput — Issue #232: highlight_term error
Author: lorenzocostalunga
Created: 2011-12-07 13:20:22+00:00
State: open

if query contains '+' highlight_term throw error.

Solution:

// LORENZO
function encodeRE(s) { return s.replace(/[.*+?^${}()|[\]\/\\]/g, '\\$0'); }
// LORENZO

// Highlight the query part of the search term
function highlight_term(value, term) {
//return value.replace(new RegExp("(?![^&;]+;)(?!<[^<>]*)(" + term + ")(?![^<>]*>)(?![^&;]+;)", "gi"), "$1"); //LORENZO
return value.replace(new RegExp("(?![^&;]+;)(?!<[^<>]*)(" + encodeRE(term) + ")(?![^<>]*>)(?![^&;]+;)", "gi"), "$1");
}

function find_value_and_highlight_term(template, value, term) {
//return template.replace(new RegExp("(?![^&;]+;)(?!<[^<>]*)(" + value + ")(?![^<>]*>)(?![^&;]+;)", "g"), highlight_term(value, term)); //LORENZO
return template.replace(new RegExp("(?![^&;]+;)(?!<[^<>]*)(" + encodeRE(value) + ")(?![^<>]*>)(?![^&;]+;)", "g"), highlight_term(value, term));
}