Is there a RegExp.escape function in JavaScript?
The function linked in another answer is insufficient. It fails to escape ^ or $ (start and end of string), or -, which in a character group is used for ranges. Use this function: function escapeRegex(string) { return string.replace(/[-\/\\^$*+?.()|[\]{}]/g, ‘\\$&’); } While it may seem unnecessary at first glance, escaping – (as well as ^) … Read more