6

In Java, you might try to make a regular expression that would match the URL stackoverflow.com using Pattern.compile("stackoverflow.com"). But this would be wrong because the . has special meaning in regular expressions. The easiest way to fix this is to write Pattern.compile(Pattern.quote("stackoverflow.com")) which comes out to: Pattern.compile("\\Qstackoverflow.com\\E") which "quotes" the entire string.

I want to do the same thing in JavaScript, but JavaScript's regular expressions don't assign any meaning to \Q and \E and it doesn't look like there is a JavaScript equivalent, so I'm not sure how to go about it. My first thought (see my answer below) is to just put a backslash before any character that has a special meaning in a JavaScript regex, but this seems potentially error-prone and I suspect that someone might know of a better way.

This is for a Firefox extension so Mozilla-specific solutions are okay.

Tyler
  • 21,762
  • 11
  • 61
  • 90
  • For anyone who comes across this in the future and is curious, I ended up solving it a different way. I wanted a regex that matched `"` followed by a particular string, followed by `"`. I ended up just searching for something like `/"[^"]+"/` (quote, a bunch of non-quote characters, then another quote) and then going through all the matches to find the one that matched the particular string I wanted. – Tyler Jun 09 '11 at 22:58

1 Answers1

6

To elaborate on my suggestion, it would look something like this:

// A RegExp that matches all the characters which have a special meaning in a RegExp
var regexpSpecialChars = /([\[\]\^\$\|\(\)\\\+\*\?\{\}\=\!])/gi;

I'm not sure if this is quite right, since some characters (like ! and =) only seem to have special meaning in certain situations. But assuming that it is, then you would do

var quotedURL = url.replace(regexpSpecialChars, '\\$1');

to replace, for instance, all $'s with \$'s. Then you can just build the RegExp from quotedURL:

var myRegExp = new RegExp(quotedURL, 'gi');
Tyler
  • 21,762
  • 11
  • 61
  • 90
  • AFAIK, escaping everything but letter and digits works, too. It makes the result even less readable, but it's a bit simpler (and IIRC there was somewhere a statement that it will work even when additional chars get their special meaning, but I don't know anymore what language it was about:D). – maaartinus Aug 05 '12 at 02:46