Error while minifing JS with regular expression in asp.net bundling

1.1k Views Asked by At

I am getting the error below while bundling JS files in my ASP.NET MVC 5 application

/* Minification failed. Returning unminified contents.
(3828,34-35): run-time error JS1013: Syntax error in regular expression: .
(3789,41-42): run-time error JS1013: Syntax error in regular expression: .

The regular expression at 3828 is

function formatPhoneNumber(value) {
       value = value.replace(/(\d{3})(\d{3})(\d{4})/, "$1-$2-$3");
       return value;
}

at 3789

var isValid = /^[0-9,.$-\s\n]*$/.test(value);

Both expressions are working fine, but not sure why bundling cannot minify them

Update 1
common.js has the regular expressions

 bundles.Add(new ScriptBundle("~/Scripts/js").Include(
                "~/Scripts/jquery.extensions.js",
                "~/Scripts/common.js",
                "~/Scripts/format.js"));

and in _layout.cshtml

 @Scripts.Render("~/Scripts/js")
1

There are 1 best solutions below

0
On

I found it There was no issue with the formatPhoneNumber. However there was issue with isValid regular expression. The error was You can not create a range with a shorthand escape sequences

So I replaced /^[0-9,.$-\s\n]*$/ with /^[0-9,.$\-\s\n]*$/

https://regex101.com/ very helpful. Actually shows the error message.