Skip to main content
edited body
Source Link

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: RegexpRegExp.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);
}

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);
}

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);
}
added 71 characters in body
Source Link

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);
}

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, 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);
}

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);
}
Source Link

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, 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);
}