First, /pattern/
is the literal notation of a RegExp object and not a string, "/pattern/"
is a string and nothing more.
If you want to concatenate several strings to build your pattern and then to obtain a RegExp object, you need to use the constructor:
var re = new RegExp('^' + prefix + command + '(?!\\S)');
return re.test(text);
(Note that when you pass a string to the RegExp constructor, you need to escape the backslashes, since to figure a literal backslash in a string you need to escape it.)
(test
is a RegExp
method, not a String
method: RegExp.prototype.test()
)
About special characters: -
, }
and ]
are not special characters and don't need to be escaped.
Note that {
is read as a literal character too, but only if it isn't the start of a quantifier {n}
, {m,n}
, {m,}
. Except for these special situations, you don't need to escape it when you write a pattern by hand, but here it's easier to escape it systematically instead of testing if it is or not the start of a quantifier. (if the escape is useless, it will be ignored)
Since you will use the RegExp constructor with a string as first parameter (Since ECMAScript 6, this parameter can also be in literal notation), you no longer need to escape the delimiter /
that is only used in the literal notation. You can remove it too:
var prefix = commandPrefix.replace(/[.?*+\\|{()[^$]/g, '\\$&');
About the readability, no need to make things more complicated than they are, a simple comment before the line should suffice.
Since escaping special regex characters is a basic task, and if you project to use it several times, you can build a function:
function regEscape(mystr) {
return mystr.replace(/[.?*+\\|{()[^$]/g, '\\$&');
}
or, why not, adding it to the String
methods:
String.prototype.regEscape = function() {
return this.replace(/[.?*+\\|{()[^$]/g, '\\$&');
};
...
function ... (...) {
...
var re = new RegExp('^' + commandPrefix.regEscape() + command + '(?!\\S)');
return re.test(text);
}
-
,]
and}
that are not special characters. Readability is a very relative idea, what is more readable: 2 lines of straight forward code or 15 lines for the same thing ( in a "readable" way)? IMO, the only thing you can do to improve the readability is to put a comment before the two lines. (assuming it could be useful for someone) \$\endgroup\$RegExp
constructor that can take a string as parameter. You can't write something like"/^" + prefix + command +...
, a RegExp instance isn't a string. As an aside Code Review is for working code. \$\endgroup\$(?=\s|$)
to(?!\S)
\$\endgroup\$\b
unless(?!\S)
is exactly needed (i.e. words are delimited by whitespaces only, but not by punctuation). \$\endgroup\$