Search code examples
javascriptjqueryjquery-uijquery-pluginsjquery-tokeninput

Jquery :invalid regular expression


i am having trouble with jquery code of the "jquery_token_inpuy"

return template.replace(new RegExp(
    "(?![^&;]+;)(?!<[^<>]*)(" + value + ")(?![^<>]*>)(?![^&;]+;)", "g"
), highlight_term(value, term));

i have this error : SyntaxError: Invalid regular expression: / Nothing to repeat.

What is the problem?


Solution

  • 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)
    );