How to get transform values using regular expressions?
How to get transform values using regular expressions?
How to get the transform values using regular expression. I have tried but it's not working for all the scenarios:
var transform = "translate(97.4 12)";
var split = /translate(s*([^s,)]+)[ ,]([^s,)]+)/.exec(transform);
/*
it given following output,
split[1] = 97.4;
split[2] = 12;
*/
But this regular expression not working for following cases:
"translate(97.4, 12)";
"translate(97.4)";
"translate(97.4,12)";
How to get those scenario also using generic way?
Given there is an API that will get you the values this is like asking for a hammer to knock in a screw.
– Robert Longson
Sep 2 at 7:09
2 Answers
2
Try this:
translate((-?d+.?d*),?s*(-?d+[.]?d*)?)
From regex101.com the explanation of the regex is as follows

not working in this scenario, "translate(97.4)" it shows 97. and 4
– Akbar Basha
Sep 2 at 4:11
You are correct! I updated the regex so that the second term is conditional (added a
? to the second capturing group)– seebiscuit
Sep 2 at 4:35
?
while giving - value also not working? "translate(13.16976047699518, -5.699114753939966)"
– Akbar Basha
Sep 2 at 4:54
Yup, I updated to handle your case as well. You should also update your Question with this use case
– seebiscuit
Sep 2 at 5:03
try this:
var transform = 'translate(97.4, 12)';
var reg = new RegExp(/translate(s*([0-9.,s)]+))/g);
var value = htmlData.replace(reg, function (a, b, c)
return b;
);
value.split(','); //return value is: [97.4, 12]
also,
for testing its good to try
https://www.regexpal.com/
very good and useful site for regex testing
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Your question doesn't have enough details and doesn't provide what you've tried or barely any explanation on what you're wishing to accomplish. Please read the making a good question guide and edit this post.
– Kwright02
Sep 2 at 3:55