0

I really tried my best but somehow I cannot make it work.

I have the following SVG path data:

var str = "m 0.05291667,1.7345817 h 0.16018 V 0.05291667 H 1.4943367 v 0.56054601 l -0.16015,0.16017899 0.16015,0.16012501 V 1.4943397 H 0.69354668 V 1.2541247 H 1.2540967 V 1.0138577 L 1.0939467,0.90175268 H 0.69354668 v -0.240215 H 1.0939467 l 0.16015,-0.128138 V 0.29313166 H 0.45330668 V 1.7345817 H 1.6544867"

What I want to achieve is to get all values that come after each H and increase this value by a number (let's say 2).

The regex I currently have selects all these numbers:

/(?<=H )(.*?)(?= )/g

However, I don't know how I can increase these numbers now using the replace() function.

This is what I currently have:

var H = str.match(/(?<=H )(.*?)(?= )/g)

for (var j=0; j<H.length; j++) {
    str = str.replace(/(?<=H )(.*?)(?= )/g, function (match, i, original) {
            nth++;
            return (nth == j) ? (parseFloat(H[j]) + 2).toString() : match;
    });
}

But it does not give me the desired result.

What can I do?

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
JoeBe
  • 1,224
  • 3
  • 13
  • 28

4 Answers4

2

Use match parameter

var str = "m 0.05291667,1.7345817 h 0.16018 V 0.05291667 H 1.4943367 v 0.56054601 l -0.16015,0.16017899 0.16015,0.16012501 V 1.4943397 H 0.69354668 V 1.2541247 H 1.2540967 V 1.0138577 L 1.0939467,0.90175268 H 0.69354668 v -0.240215 H 1.0939467 l 0.16015,-0.128138 V 0.29313166 H 0.45330668 V 1.7345817 H 1.6544867"

str = str.replace(/(?<=H )(.*?)(?= )/g, function(match, i, original) {
  return parseFloat(match) + 2
});

console.log(str)
User863
  • 19,346
  • 2
  • 17
  • 41
1

You could make use of the callback function from replace and use 2 capturing groups.

Note that you don't need a lookbehind (?<= and that lookbehinds are not yet widely supported in Javascript.

You could match the numbers and the capital H using:

\b(H )(\d+(?:\.\d+)?)
  • \b(H ) Capture group 1, match H and a space preceded by a word boundary
  • ( Capture group 2
    • \d+(?:\.\d+)? Match 1+ digits, optionally match a dot and 1+ digits
  • ) Close group

Regex demo

In the replacement use the 2 capturing groups and add a number to what is captured in group 2.

let pattern = /\b(H )(\d+(?:\.\d+)?)/g;
var str = "m 0.05291667,1.7345817 h 0.16018 V 0.05291667 H 1.4943367 v 0.56054601 l -0.16015,0.16017899 0.16015,0.16012501 V 1.4943397 H 0.69354668 V 1.2541247 H 1.2540967 V 1.0138577 L 1.0939467,0.90175268 H 0.69354668 v -0.240215 H 1.0939467 l 0.16015,-0.128138 V 0.29313166 H 0.45330668 V 1.7345817 H 1.6544867"
str = str.replace(pattern, function(_, g1, g2) {
  return g1 + (Number(g2) + 2);
});
console.log(str);
The fourth bird
  • 154,723
  • 16
  • 55
  • 70
0
const str = "m 0.05291667,1.7345817 h 0.16018 V 0.05291667 H 1.4943367 v 0.56054601 l -0.16015,0.16017899 0.16015,0.16012501 V 1.4943397 H 0.69354668 V 1.2541247 H 1.2540967 V 1.0138577 L 1.0939467,0.90175268 H 0.69354668 v -0.240215 H 1.0939467 l 0.16015,-0.128138 V 0.29313166 H 0.45330668 V 1.7345817 H 1.6544867"

const regex = new RegExp('H\\s*([\\d\\.]+)', 'g');
let matches;
while ((matches = regex.exec(str))) {
    const found = matches[1];
    const newValue = parseFloat(found) + 2.0;
    console.log(newValue);
}
Scriptkiddy1337
  • 792
  • 4
  • 9
0

You should try with a regex replace

str.replace( new RegExp("(?<=H )(.*?)(?= )","g"), function(match) { 
    return parseFloat(match) + 2;
}):
Florent Descroix
  • 579
  • 4
  • 13