You could use a routine to "quote" all the meta-characters. Something like this would be a start:
function regexSanitize( str ) {
return str.replace(/([.+*?:\[\](){}|\\])/g, "\\$1");
}
then:
return template.replace(
new RegExp(
"(?![^&;]+;)(?!<[^<>]*)(" +
regexSanitize(value) +
")(?![^<>]*>)(?![^&;]+;)", "g"),
highlight_term(value, term)
);