In my current task, I'm animating the coordinates of SVG paths, so I'm trying to programmatically alter their values depending on an offset that the user changes. The coordinates are in ugly attributes whose values looks something like this:
M0,383c63,0,89.3,-14.1,117.4,-65.9...
For an offset of 100, the new value might need to look something like:
M0,483c63,0,89.3,-14.1,217.4,-65.9... // without the bold (it's just there to highlight diff)
I pass in different regular expressions for different paths, which look something like, say, /long(N)and...N...ugly(N)regex/
, but each path has a different amount of captures (I can pass in that quantity too). Using the answers here and a loop, I figured out how to programmatically make an array of newly changed values (regardless of the number of captures), giving me something like ['483','217.4',...]
.
My question is, how do I programmatically replace/inject the altered numbers back into the string (as in the line above with the bolded numbers)?
My workaround for the moment (and a fiddle ):
I'm generating a "reverse" regex by switching parens around, which ends up looking like /(long)N(and...N...ugly)N(regex)/
. Then I'm doing stuff by hand, e.g.:
if (previouslyCapturedCount == 3) {
d = d.replace(reverseRE, "1ドル" + dValuesAltered[0] + "2ドル" + dValuesAltered[1] + "3ドル" + dValuesAltered[2] + "4ドル");
} else if (previouslyCapturedCount == 4) {
// and so on ...
But there's gotta be a better way, no? (Maybe due to my lack of JS skillz, I simply wasn't able to extrapolate it from the answers in the above linked question.)
Caveat: There are several questions here on S.O. that seem to answer how to do multiple replacements on specific pairs, but they seem to assume global replacements, and I have no guarantee that the matched/captured numbers will be unique in the string.
-
1Please provide an MVCE (minimal complete verifiable example).Wiktor Stribiżew– Wiktor Stribiżew2016年05月02日 19:57:25 +00:00Commented May 2, 2016 at 19:57
-
1@WiktorStribiżew isn't MCVE intead? ;)Pedro Lobito– Pedro Lobito2016年05月02日 20:26:23 +00:00Commented May 2, 2016 at 20:26
-
Six of one, half a dozen of the otherWiktor Stribiżew– Wiktor Stribiżew2016年05月02日 20:29:29 +00:00Commented May 2, 2016 at 20:29
-
Added a fiddle to hopefully satisfy the MCVE requirement. Sorry if the abstraction/elision in the question is obnoxious. It's just that I often find those "wall of code" questions obnoxious. :-)Max Starkenburg– Max Starkenburg2016年05月02日 20:37:34 +00:00Commented May 2, 2016 at 20:37
1 Answer 1
Rather than making a huge complex pattern to find and replace everything you're looking for, I'd iterate each item and replace them one at a time. With a regex that looks like this:
In this regex the {1}
is the number of comma delimited values you'd like to skip over. In this case we're skipping the first and replacing just the leading numbers in the second string.
^(,?[^,]*){1},([0-9]+)
Replace With the following, where _XX_
is the desired new value.
1,ドル_XX_
4 Comments
if (captureCount == 3) { ...
for each captureCount, instead of something more like a one-liner). I guess my larger/general question was whether it's possible, given a regex, to replace the captures with slightly altered values, programmatically. And I thought making that array got me halfway there (though maybe it was an unnecessary step).