0

So I want to match in string bellow, which will be formula for excell/ spreadsheet type cell, all addresses: \w+\d+ , and change numbers only number part in them. I want to get following strings from original: "= A2 + B2", "=A3+B3", "=A4+B4" ...

I tried:

const a = "=A1 + B1"
for (let i = 0; i < 100 ; i++) {
    const b = a.replace(/\w+$(\d+)/g, String(i + 1));
    console.log(b)
}
    

and it gives result:

enter image description here

then if I do without $ before grouping () parentesis:

const a = "=A1 + B1"
for (let i = 0; i < 100 ; i++) {
    const b = a.replace(/\w+(\d+)/g, String(i + 1));
    console.log(b)
}

I get:

enter image description here

4
  • Given the input "=A1 + B1", what result do you want? Commented Jan 2, 2023 at 13:16
  • So I want number (which in string is 1, but can be anything, like 24, 67, ) to be changed with index i + 1 in for loop. Commented Jan 2, 2023 at 13:18
  • So "=A27 + B42" becomes ="A1 + B1", ="A2 + B2", etc.? Commented Jan 2, 2023 at 13:21
  • yes, But the idea is that user insert string =A1 + B1, and it will be adapted for differant cell adresses with this regex. Commented Jan 2, 2023 at 13:25

4 Answers 4

1

Something like this?

const a = "=A1 + B1"
for (let i = 0; i < 10 ; i++) {
    const b = a.replace(/\b\w+\d+\b/g, function(match) {
        const num = match.match(/\d+/);
        const newNum = Number(num[0]) + i;
        return match.replace(/\d+/, newNum);
    });
    console.log(b);
}

Sign up to request clarification or add additional context in comments.

Comments

1

From your comment, it sounds like you want 100 results, replacing the row numbers with 1, 2, 3, etc.

If so, capture the column number and use it in the replacement:

function updateFormula(formula, i) {
    const result = formula.replace(
        /\b([A-Z]+)\d+\b/g,
        (m, column) => column + i
    );
    return result;
}

const formula = "=A42 + B27";
for (let n = 1; n <= 100; ++n) {
    const result = updateFormula(formula, n);
    // ...
}

Live Example (only doing 20 because the in-snippet console drops rows after a while):

function updateFormula(formula, i) {
    const result = formula.replace(
        /\b([A-Z]+)\d+\b/g,
        (m, column) => column + i
    );
    return result;
}

const formula = "=A42 + B27";
for (let n = 1; n <= 20; ++n) {
    const result = updateFormula(formula, n);
    console.log(`${JSON.stringify(formula)} => ${JSON.stringify(result)}`);
}
.as-console-log {
    max-height: 100% !important;
}

Comments

0

So, all answers are really great, but in code I decided to implement something similar to Joan Lara Ganau answere.

const a = "=A1 + B1"
for (let i = 0; i < 100 ; i++) {
    const num = String(i+1);
    const b = a.replace(/\w+\d+/g, function(match) {
        return match.replace(/\d+/g, num)
    });
    console.log(b);
}

which produces :

enter image description here

Still don't know why we need \b, is it similar to ()?

Comments

0

Just match the digits after a letter.

const a = "=A1 + B1";
for (let i = 0; i < 10; i++) {
  const b = a.replace(/(?<=[A-Z]+)\d+/g, i + 1);
  console.log(b);
}

Or you can use split the string to get the pattern, so you don't have to match it on every iteration.

const a = "=A1 + B1"
const parts = a.split(/(?<=[A-Z]+)\d+/g);
for (let i = 0; i < 10; i++) {
  const b = parts.join(i + 1);
  console.log(b);
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.