JavaScript minification fails containing certain Regex - javascript

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.

Related

Getting Lexer Error with AngularJS and ng-pattern

I'm getting the following error at runtime:
Error: [$parse:lexerr] Lexer Error: Unexpected next character at columns 11-11 [\] in expression [[0-9]{1,4}(\.[0-9]{1,3})?].
My HTML looks like this
<input type="text"
class="form-control"
name="amount"
ng-pattern="{{ctrl.pattern}}"
ng-model="ctrl.amount">
The assignment in my AngularJS controller is as follows:
ctrl.pattern = '[0-9]{1,4}(\\.[0-9]{1,3})?';
From what I understand AngularJS appends the ^ and $ at the beginning and end of regular expressions and that works great. The problem is that the regex literal in the middle of the expression is not being accepted. I want to dot to be accepted as a literal and not as any character so I need a way to escape it yet the lexer does not to like it. Is there a way around this?
This issue is already reported here but they are not going to fix it as according to them its a uncommon usecase.
Do it this way instead:
ctrl.pattern = /^[0-9]{1,4}([.][0-9]{1,3})?$/;
This way the regex will be evaluated as a regex object instead of a string parameter to RegExp in which case we will need to add ^,$

unterminated regular expression literal js error

I have added jQuery validator add method for the presence of http check in url field.
JS
jQuery.validator.addMethod("valid_url", function(value, element) {
if(!/^(https?|ftp):\/\/i.test(val))
val = 'http://'+val; // set both the value
$(elem).val(val); // also update the form element
}
My Console throws
"unterminated regular expression literal" error in the following line.
if(!/^(https?|ftp):\/\/i.test(val))
What my mistake is?
Regular expression literals should be surrounded by the delimiters(/). There's no terminating delimiter:
/^(https?|ftp):\/\//i
// ^
For example:
>> /^(https?|ftp):\/\//i.test('http://stackoverflow.com/')
true
>> /^(https?|ftp):\/\//i.test('telnet://stackoverflow.com/')
false
You can also get this error if you're simply attempting to concatenate a variable that utilizes a regular expression and forget a quotation mark somewhere.

jmeter code to remove crlf from an extracted Url

I need to remove trailing crlf from a string in jmeter. I have used the following function
${__javaScript('${varu_2}'.replace(/[\\r\\n]/g\,""))}
It gives me error "org.mozilla.javascript.EvaluatorException:
unterminated string literal (#1)".
Any help would be appreciated.
The inline evaluation (${varu_2}) of your variable containing multiple lines is broking the javascript evaluation.
For example the javascript would be interpreted as :
'line1
line2'.replace(/[\\r\\n]/g\,"")
The first line is an unterminated string like the error message tells us.
To avoid this behavior I would recommend using the JMeterVariables object, but since the javascript implementation (Rhino) does not seems to support well inline regexp, you should use 2 replace function instead of your regular expression:
${__javaScript(vars.get('varu_2').replace( "\r"\, "" ).replace("\n"\,""))}
Or if you prefer, with a beanshell function you can use any regular expression you :
${__BeanShell(vars.get("varu_2").replaceAll("[\r\n]"\,""))}

JsHint warning: A regular expression literal can be confused with '/='

I have this line in my Javascript code:
var regex = /===Hello===\n/;
JsHint gives me a warning in this line:
A regular expression literal can be confused with '/='`
...but I don't know what's wrong with this regular expression? How can I avoid this warning?
The problem is that /= could be interpreted as a division and assignment, rather than the start of a regular expression literal.
You can avoid the warning by using the RegExp constructor instead:
var regex = new RegExp("===Hello===\n");
There doesn't appear to be any option you can set to tell JSHint (or JSLint for that matter) to ignore /=, so your choice is either to work around it or ignore the warning.

javascript regex error

I have this string:
£1,134.00 (£1,360.80 inc VAT)
And I am trying to extract the numbers to get the following:
['1,134.00','1,360.80']
Using the following regex pattern in Javascript:
/\d*,?\d+\.\d{2}/g
It is working fine in Chrome, but I get this error in Opera:
Uncaught exception: Syntax error, unrecognized expression: (£1,360.80 inc VAT)
Error thrown at line 75, column 784 in <anonymous function: k.error>(g) in http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js:
throw"Syntax error, unrecognized expression: "+g;
Obviously I would like it to work in all modern browsers but I have no idea what is causing this. I have also tried several other regex patterns and have looked into escape characters as I thought it might be that.
Any ideas?!
Let me know if more info is needed. Thanks
unrecognized expression: (£1,360.80 inc VAT) <= that's not an error in your regex. Your string is not being a string. Somehow it's getting mixed and interpreted as part of your javascript.

Categories

Resources