Length regex tests true on null [duplicate] - javascript

This question already has answers here:
Is it a bug in Ecmascript - /\S/.test(null) returns true?
(2 answers)
Closed 9 years ago.
I need to verify that an input is between 1 and 512 characters long. I'm using the standard length-check regex of /^.{1,512}$/. When I run
/^.{1,512}$/.test(null)
it returns true. How do I get a length-check regex to fail against null? And why does this test true against null in the first place?
EDIT: Leaving this here since it's more googleable in my case than the earlier question, but per here, the problem is that the regex coerces null into 'null' before testing.

I would test with a regex like this:
var myregexp = /^(?!null).{1,512}$/m;;

Related

Javascript Boolean Validation [duplicate]

This question already has an answer here:
(![]+[])[+[]]... Explain why this works
(1 answer)
Closed 4 years ago.
My question is about boolean validation of below expression.
If you run (!+[]+[]+![]) in your JS console, it returns us 'truefalse'.
How is it possible? How does this logic work?
First part !+[] returns true as a Boolean. Second part []+![] is "false" as String. Concatenating Boolean with String converts the result to string and gives you at the end "truefalse".
Here is provided deep explanation to JSFuck
https://github.com/aemkei/jsfuck#how-it-works

How can '+myVar;' be a valid syntax [duplicate]

This question already has answers here:
Explain +var and -var unary operator in javascript
(7 answers)
Closed 6 years ago.
I just got a bug that took some of my time to spot my searching filters weren't working because of the following code :
queryObject.search='valid==true';+searchQuery;
The good syntax is to mive the ';' in the string :
queryObject.search='valid==true;'+searchQuery;
The reasn why i didn't spot that is because the earlier line of code didn't triggered any javascript console error. So it seems it's a valid syntax.
So here is my question, how can this be a valid syntax ?
+something is an expression using the plus unary operator.
Its general purpose is to convert a value, for example a string, to a number.
+ is unary operator, which tries to get numeric value from variable.
There is a thread about it.

Why javascript indexOf "empty string" always returning zero [duplicate]

This question already has answers here:
indexOf empty string is zero. why?
(1 answer)
Java String.indexOf and empty Strings
(7 answers)
Closed 8 years ago.
Why javascript indexOf method always return 0 for empty string. If i used any string indexOf("") always returns zero. Any reason for this. Explain the reason briefley.
var str="jquery";
var index=str.indexOf("") \\ Always returns zero for any string.
Thanks in advance.

Comma separated number/function in parenthesis in JavaScript? [duplicate]

This question already has answers here:
What does the comma operator do in JavaScript?
(5 answers)
Closed 6 years ago.
I read a line from doT.js:
var global = (function(){ return this || (0||eval)('this'); }());
After it was minified:
l=function(){return this||(0,eval)("this")}();
So what is the (0,eval), I mean what does the comma do?
I played in Chrome's console, (0,1), (2,1), (2,{}), 2,1, etc, it always returns the last one.
The comma operator evaluates both and always returns the last. Much like you said.
You can read up on the comma operator: http://javascriptweblog.wordpress.com/2011/04/04/the-javascript-comma-operator/
Even though I have no idea the purpose of (0||eval)... (0,eval) is the equivalent and one less character.

why does my javascript regex.test() give alternating results [duplicate]

This question already has answers here:
Why does a RegExp with global flag give wrong results?
(7 answers)
Closed 5 years ago.
Possible Duplicate:
Javascript regex returning true.. then false.. then true.. etc
var r = /\d/g;
var a = r.test("1"); // will be true
var b = r.test("1"); // will be false
console.log(a == b); // will be false
Please explain to me why the result of r.test("1") alternates with each call?
I was able to work around the issue I was having by removing the g modifier. However I would still like to understand why this happens.
When you're using /g, the regex object will save state between calls (since you should be using it to match over multiple calls). It matches once, but subsequent calls start from after the original match.
(This is a duplicate of Javascript regex returning true.. then false.. then true.. etc)

Categories

Resources