[removed] negative lookbehind equivalent?

前端 未结 12 1559
南旧
南旧 2020-11-21 05:47

Is there a way to achieve the equivalent of a negative lookbehind in javascript regular expressions? I need to match a string that does not start with a specific set of cha

12条回答
  •  春和景丽
    2020-11-21 06:07

    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:

    ((?!([abcdefg])).|^)m
    

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

提交回复
热议问题