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.