This is interesting...
Code: Select allvar s = "100px";
var r = /(%|px|mm|cm)/;
alert(s.search(r)); // returns 3
alert(s.search(r)); // returns -1 !
alert(s.search(/(%|px|mm|cm)/)); // returns 3
alert(s.search(/(%|px|mm|cm)/)); // returns 3
regex mystery
-
Mikaeru
regex mystery
On Photoshop CS4 (11.0.2) on Mac OS X 10.6.8, I get too: 3, -1, 3, 3 respectively.
What is interesting is the following code which tends to indicate that, when the regex is passed by variable, the last index position is internally kept between the calls as a convenience, since global search (ie: "g" flag) is not supported. But, AFAIK, this is an undocumented, non-standard feature and it appears to have been removed in more recent versions...
Code: Select allvar s1 = "100px 50mm 25%";
var r = /(%|px|mm|cm)/;
alert(s1.search(r)); // returns 3
alert(s1.search(r)); // returns 8
alert(s1.search(r)); // returns 13
alert(s1.search(r)); // returns -1
What is interesting is the following code which tends to indicate that, when the regex is passed by variable, the last index position is internally kept between the calls as a convenience, since global search (ie: "g" flag) is not supported. But, AFAIK, this is an undocumented, non-standard feature and it appears to have been removed in more recent versions...
Code: Select allvar s1 = "100px 50mm 25%";
var r = /(%|px|mm|cm)/;
alert(s1.search(r)); // returns 3
alert(s1.search(r)); // returns 8
alert(s1.search(r)); // returns 13
alert(s1.search(r)); // returns -1