regex mystery

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: Tom, Kukurykus

kpt

regex mystery

Post by kpt »

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

Professional AI Audio Generation within Adobe Premiere Pro - Download Free Plugin here

pfaffenbichler

regex mystery

Post by pfaffenbichler »

I get »3« throughout.
kpt

regex mystery

Post by kpt »

Interesting. I'm on Photoshop CS5.1 12.1 on a Mac, what are you using?
pfaffenbichler

regex mystery

Post by pfaffenbichler »

Photoshop 13.0.2 on Mac OS 10.6.8
Mikaeru

regex mystery

Post by Mikaeru »

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
kpt

regex mystery

Post by kpt »

That makes perfect sense - nice research, thanks!