12

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.

1
  • 1
    Similar to a question I asked a few months ago! I never really got an answer though so maybe you will... stackoverflow.com/questions/3614440/… Commented Jun 9, 2011 at 22:55

2 Answers 2

23
new RegExp(test.replace(/[#-.]|[[-^]|[?|{}]/g, '\\$&'));

This escapes the following:

  • The characters between # and ., so: #$%&'()*+,-.
  • The four characters [, \, ], and ^
  • These characters ?, |, {, }

...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.

Sign up to request clarification or add additional context in comments.

3 Comments

you forgot the /g global flag, to replace all the occurrences (I tried to edit your post, but it tells me that the change must be at least 6 characters)
I had some issues with the second one (the regex would not match some things it should for some reason), but the first one works great!
Apparently, the short form doesn't work on Qt 5.15, so it might be a compatibility issue.
2

This will replace all special RegExp characters with their escaped version:

test = test.replace(/[\\\.\+\*\?\^\$\[\]\(\)\{\}\/\'\#\:\!\=\|]/ig, "\\$&");

6 Comments

But some of those characters are only special in certain contexts, aren't they? Does that cause a problem at all?
In other contexts escaping the character won't harm the RegExp match string, it will just end up being unnecessary.
no need to escape #, /, ' or :. You also need - and ,, and you don't need to escape most of those characters in the context you're in. It can simply become /[-\\.,_*+?^$[\](){}!=|]/
Also no need for case insensitive /ig in answer just /g. @MarkKahn why comma and underscore?
, is for repetition ranges: {1,3}. Not sure why I added an underscore, it was 6 years ago :D Seems unnecessary
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.