var stringToHighlight = [userinput] // may be any string like "foo", "bar" or "."
var stringToBeHighlighted = [any text] // Lorem Ipsum ...
So far I have
var regex = new RegExp(stringToHighlight, "g")
var highlightedString = stringToBeHighlighted.replace(regex, "<span class='highlight'>$&</span>")
This doesn't work for the character "." for example, because it is being interpreted as the regular expression metacharacter . but not the actual character "." resulting in all the text being highlighted. How do I exclude those special metacharacters?