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:
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:
CodePudding user response:
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);
}
CodePudding user response:
Still don't know why we need \b, is it similar to ()?
CodePudding user response:
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);
}