There is a line:
=小校袦袦(A18:I18)

You need to write a regular expression where you would search for all the digits of 18 in the line, except for those that have the letter A in front of the 18 .

I am writing /(?<=A)+18/g - gives an error Invalid quantifier ? .

  • 2
    extra + in regular expression. Plus, 18 are now displayed, which are after A - Grundy
  • Isn't it easier to write like this: /[^A](18)/g ? - And
  • @And, why did you turn 18 into a separate group? - Let's say Pie
  • one
    /(?<![A\d])18(?!\d)/g ? - Wiktor Stribi偶ew
  • in js for some reason? <! does not work (((( - Yaroslav Work Tolstoy

1 answer 1

In Chrome and any other environment that supports the ECMA2018 standard, you can use the backward preview block:

 console.log("=小校袦袦(A18:I18)".match(/(?<![A\d])18(?!\d)/g)) console.log("=小校袦袦(A18:I18)".replace(/(?<![A\d])18(?!\d)/g, "<孝校孝>$&</孝校孝>")) 

Details

  • (?<![A\d]) - immediately before 18 there should not be A or numbers
  • 18 - 18
  • (?!\d) - right after 18 there should not be a digit

In other cases, you have to solve the problem with the help of exciting groups:

 var s = "=小校袦袦(A18:I18)"; var rx = /([A\d])?18(?!\d)/g; console.log(s.replace(rx, function ($0, $1) { return $1 ? $0 : "<孝校孝>" + $0 + "</孝校孝>" })) 

Here ([A\d])? - optional exciting group, it finds an A or a number. If this pattern captures text, the group 1 buffer is non-empty and in this case, the match 18 is valid. If the verification of the first group fails, the match is considered not found.