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?





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





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



Regex Explanation





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.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

How do I collapse sections of code in Visual Studio Code for Windows?

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ