I want to use a string to perform a global regex, but it might have regex characters in it. What's the best way to escape all regex characters in a string before building a regex with it?
Basically I might have something like this;
var test = 'test.';
var regex = new RegExp(test, 'ig');
I need 'test.' to become 'test\.' so it doesn't behave in unexpected ways.
new RegExp(test.replace(/[#-.]|[[-^]|[?|{}]/g, '\\$&'));
This escapes the following:
#
and .
, so: #$%&'()*+,-.
[
, \
, ]
, and ^
?
, |
, {
, }
...by prepending a backslash before each occurrence (as $&
Inserts the matched substring, and the g
flag means to replace all instances.)
Alternatively, if you're looking for a shorter version, try:
new RegExp(test.replace(/[#-}]/g, '\\$&'));
This will end up escaping a lot more than it should, but it won't harm anything.