I just want to create a regular expression out of any possible string.
var usersString = "Hello?!*`~World()[]";
var expression = new RegExp(RegExp.escape(usersString))
var matches = "Hello".match(expression);
Is there a built in method for that? If not, what do people use? Ruby has RegExp.escape
. I don't feel like I'd need to write my own, there's gotta be something standard out there. Thanks!
Most of the expressions here solve single specific use cases.
That's okay, but I prefer an "always works" approach.
This will "fully escape" a literal string for any of the following uses in regular expressions:
new RegExp(regExpEscape(str))
new RegExp('[' + regExpEscape(str) + ']')
new RegExp('x{1,' + regExpEscape(str) + '}')
Special Characters Covered:
-
: Creates a character range in a character class.[
/]
: Starts / ends a character class.{
/}
: Starts / ends a numeration specifier.(
/)
: Starts / ends a group.*
/+
/?
: Specifies repetition type..
: Matches any character.\
: Escapes characters, and starts entities.^
: Specifies start of matching zone, and negates matching in a character class.$
: Specifies end of matching zone.|
: Specifies alternation.#
: Specifies comment in free spacing mode.\s
: Ignored in free spacing mode.,
: Separates values in numeration specifier./
: Starts or ends expression.:
: Completes special group types, and part of Perl-style character classes.!
: Negates zero-width group.<
/=
: Part of zero-width group specifications.Notes:
/
is not strictly necessary in any flavor of regular expression. However, it protects in case someone (shudder) doeseval("/" + pattern + "/");
.,
ensures that if the string is meant to be an integer in the numerical specifier, it will properly cause a RegExp compiling error instead of silently compiling wrong.#
, and\s
do not need to be escaped in JavaScript, but do in many other flavors. They are escaped here in case the regular expression will later be passed to another program.If you also need to future-proof the regular expression against potential additions to the JavaScript regex engine capabilities, I recommend using the more paranoid:
This function escapes every character except those explicitly guaranteed not be used for syntax in future regular expression flavors.
For the truly sanitation-keen, consider this edge case:
This should compile fine in JavaScript, but will not in some other flavors. If intending to pass to another flavor, the null case of
s === ''
should be independently checked, like so:Nothing should prevent you from just escaping every non-alphanumeric character:
You lose a certain degree of readability when doing
re.toString()
but you win a great deal of simplicity (and security).According to ECMA-262, on the one hand, regular expression "syntax characters" are always non-alphanumeric, such that the result is secure, and special escape sequences (
\d
,\w
,\n
) are always alphanumeric such that no false control escapes will be produced.This is a shorter version.
This includes the non-meta characters of
%
,&
,'
, and,
, but the JavaScript RegExp specification allows this.Mozilla Developer Network's Guide to Regular Expressions provides this escaping function:
XRegExp has an escape function:
XRegExp.escape('Escaped? <.>'); // -> 'Escaped\?\ <\.>'
More on: http://xregexp.com/api/#escape
Rather than only escaping characters which will cause issues in your regular expression (e.g.: a blacklist), why not consider using a whitelist instead. This way each character is considered tainted unless it matches.
For this example, assume the following expression:
This whitelists letters, number and spaces:
Returns:
This may escape characters which do not need to be escaped, but this doesn't hinder your expression (maybe some minor time penalties - but it's worth it for safety).