While using regex to match values inside of a formula string I found this issue that regex would return null even though there is a match.
var formula = "round((DATAtotal_sq_ft * .6) + (QTY16 * 4) + (QTY17 * 2) + QTY18 + QTY15 + QTY12 * 18 / 3000, 1)";
const qtyRegex = /(QTY)(\d*)|(LEN)(\d*)|(DATA)([a-zA-Z_-|\d]*)/gm;
let m;
while ((m = qtyRegex.exec(formula)) !== null) {
var val = 0; // Here is irrelevant code that gets the value
formula = formula.replace(m[0], val);
}
console.log(formula);
In the above snippet you can see the result that a couple of the values don't get replaced but at the same time all of them get detected by regex101 https://regex101.com/r/WTpvFq/1. For some reason I cant seem to narrow down what I'm doing wrong even after reading a number of different answers to similar problems.
I could use a workaround and use formula.match(qtyRegex) but I am really certain it's just an error in the Regex so I'd prefer properly fixing it instead of plastering it with a patch.
.replaceinside an.execloop does not look appropriate. What are you really doing? Besides,[a-zA-Z_-|\d]is an error, probably you wanted to use[\w-]