I can't minify a javascript file containing this working Regex
:
isValid = function (str) {
return !/[~`!#$%\^&*+=\-\[\]\\';,/{}|\\":<>\?]/g.test(str);
},
Error message:
/* Minification failed. Returning unminified contents.
(2128,43-44): run-time error JS1004: Expected ';': {
(2128,45-46): run-time error JS1195: Expected expression: |
(2128,46-47): run-time error JS1014: Invalid character: \
(2128,47-48): run-time error JS1014: Invalid character: \
(2128,48-70): run-time error JS1015: Unterminated string constant:
":<>\?]/g.test(str);
(2129,6-7) : run-time error JS1195: Expected expression: ,
(2128,17-43): run-time error JS5017: Syntax error in regular expression:
/[~`!#$%\^&*+=\-\[\]\\';,/
*/
Bundle Config:
bundles.Add(new ScriptBundle("~/bundlesJs/custom").Include(
"~/Scripts/Autogenerated/AntiHacking/antiHackingHelper.js"
));
BundleTable.EnableOptimizations = true;
How can this be solved? Is there a character that needs to be escaped or should I wrap the whole Regex?
Acc. to ES5 docs, the forward slash is NOT a special regex metacharacter.
JavaScript regex engine always allows an unescaped /
in a RegExp
constructor notation and you may use an unescaped /
forward slash inside character classes even when written in a regex literal notation, try this code (in Chrome, FF, and I have checked it in IE 11, too):
console.log( '1//25///6'.match(/\w[/]+\w/g) );
console.log( '1//25///6'.match(RegExp("\\w/+\\w", "g")) );
However, the best practice is to escape the regex delimiters to avoid any issues like the one you have and keep the patterns consistent and unambiguous.