Replicate the functionality of Java's "Pattern.quote" in a JavaScript RegExp [duplicate]
In Java, you might try to make a regular expression that would match the UR开发者_JAVA技巧L 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.
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');
精彩评论