javascript regex - look behind alternative?

前端 未结 6 1877
时光取名叫无心
时光取名叫无心 2020-11-22 05:44

Here is a regex that works fine in most regex implementations:

(?

This matches .js for a string which ends with .js exc

6条回答
  •  难免孤独
    2020-11-22 06:32

    Let's suppose you want to find all int not preceded by unsigned:

    With support for negative look-behind:

    (?

    Without support for negative look-behind:

    ((?!unsigned ).{9}|^.{0,8})int
    

    Basically idea is to grab n preceding characters and exclude match with negative look-ahead, but also match the cases where there's no preceeding n characters. (where n is length of look-behind).

    So the regex in question:

    (?

    would translate to:

    ((?!filename).{8}|^.{0,7})\.js$
    

    You might need to play with capturing groups to find exact spot of the string that interests you or you want't to replace specific part with something else.

提交回复
热议问题