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

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)

Related

Why are these different way of declaring regular expressions not producing the same result? [duplicate]

This question already has answers here:
Why do regex constructors need to be double escaped?
(5 answers)
Is there a RegExp.escape function in JavaScript?
(18 answers)
Closed 1 year ago.
Here's the code that declares the same pattern in two different ways
let reg = new RegExp("\d+"); // 1st way
let reg2 = /\d+/; // 2nd way
let string = "one two 100";
console.log(reg.test(string)); // false
console.log(reg2.test(string)); // true
I was reading the book "Eloquent JavaScript" and they said these two ways of declaring regex are equivalent but I don't know why the code above is producing different results.

What exactly is (alert(1),"") in javascript [duplicate]

This question already has answers here:
What does the comma operator do in JavaScript?
(5 answers)
Closed 2 years ago.
I tried doing google gruyeres XSS challenges (http://google-gruyere.appspot.com/part2), and at the stored AJAX XSS challenge they have the following code part for the JSON response:
all <span style=display:none>"
+ (alert(1),"")
+ "</span>your base
The interesting part is: (alert(1),"")
According to the solution provided, the empty string gets returned. According to my testing, the alert(1) still gets exectued.
Is this some sort of function shorthand, or what would this be called in JS?
Why does it execute the alert, but then return the empty string?
Thank you very much for any help!
Best regards,
Rolf
This is the comma operator. The code executes alert(1), discards its return value, then evaluates "". Since this is the last item in the expression, its value is returned, which is empty string.
The tutorial I linked describes it as follows:
The comma operator in JavaScript evaluates each of its operands. It returns the value of the last operand. Add multiple expressions using the comma operator.

How to prevent regular expression test toggling when using a variable? [duplicate]

This question already has answers here:
Unexpected Javascript RegExp behavior [duplicate]
(3 answers)
Closed 8 years ago.
I just noticed the following strange behavior (both in browsers and nodejs:
> a = /^\/foo/g
/^\/foo/g
> a.test("/foo")
true
> a.test("/foo")
false
> a.test("/foo")
true
> a.test("/foo")
false
> a.test("/foo")
true
What kind of mad science we have here? How can I prevent this behavior?
I just want to have a regex variable that checks if a string matches the pattern globally.
Documentation pages don't seem to bring some light there... I'd really appreciate a link to documentation references.
Set the lastIndex of your regex to 0 after each test.
a = /^\/foo/g
a.test("/foo"); //true
a.test("/foo"); //false
a.test("/foo"); //true
a.lastIndex = 0; //reset the index to start the next match at beginning
a.test("/foo"); //true

Length regex tests true on null [duplicate]

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

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.

Categories

Resources