Home > Software design >  Is there something that lookaheads do to the normal part of a regex?
Is there something that lookaheads do to the normal part of a regex?

Time:08-14

I was trying to use lookaheads in my code to try to apply a function to all numbers in a string, but when I tried to add a lookahead to prevent random words with numbers in them (like models of things) the regex only gets the last whole interger (in this case, 9) and adds the decimals. I assume I'm doing something wrong with adding the lookahead, but I couldnt find anything on it. The length of the numbers varies, so using curly brackets isn't an option. What am I doing wrong?

e = "59.84"
ea = e.replace(/(?!\w\-*\d \.?\d*\w)\-?\d \.*\d*/g, v => v * 2 );
console.log(ea)/*logs... 519.68???
5   9.84 * 2
*/

e = "59.84"
ea = e.replace(/\-?\d \.?\d*/g, v => v * 2 );
console.log(ea)/*logs 119.68*/

CodePudding user response:

The look-ahead pattern \w\-*\d \.?\d*\w matches at the first position:

  • \w matches with 5
  • -* is a zero length match
  • \d matches with 9
  • \.? matches with .
  • \d* matches with 8 (after trying first with 84 and backtracking)
  • \w matches with 4

And since this is a negative look ahead, there cannot be a match at the first position, even though the "normal" part would match. The initial digit 5 in the input will not be replaced. The first possible match starts at the next position.

I think your main mistake was to think that \w would only match Latin letters and not digits, but it does match with digits too.

To avoid matching numbers that are pre- or postfixed by letters, you can use:

var e = "59.84  test91.23  53.66test ok:12.34.";
var ea = e.replace(/(?<!\w)\-?\d (\.\d*(?!\w)|(?![\w.]))/gi, v => v * 2 );
console.log(ea);

  • Related