I wrote regular expression
/translateX\((?<X>-{0,1}\d+)px\)|translateY\((?<Y>-{0,1}\d+)px\)/g
For given string
translateX(381px) translateY(-94px)
To capture translateX and translateY values. Tested it by regex101.com service, it works well there. But, when I try it in JavaScript it capture translateX value which is 381 but not capture translateY which is -94. Do you have any ideas why is this happening?
regex101
https://i.sstatic.net/Nuj5X.png
JavaScript exec
console.log(/translateX\((?<X>-{0,1}\d+)px\)|translateY\((?<Y>-{0,1}\d+)px\)/g.exec("translateX(381px) translateY(-94px)"));
Using matchAll gives the same result.
JavaScript matchAll
console.log("translateX(381px) translateY(-94px)".matchAll(/translateX\((?<X>-{0,1}\d+)px\)|translateY\((?<Y>-{0,1}\d+)px\)/g).next().value.groups);
{0,1}is usually written as?to indicate an optional pattern..next()once in the last example, so you only get the first match. Try converting it to an array and you'll get all of the matches.