[removed] negative lookbehind equivalent?

前端 未结 12 1558
南旧
南旧 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

    Mijoja's strategy works for your specific case but not in general:

    js>newString = "Fall ball bill balll llama".replace(/(ba)?ll/g,
       function($0,$1){ return $1?$0:"[match]";});
    Fa[match] ball bi[match] balll [match]ama
    

    Here's an example where the goal is to match a double-l but not if it is preceded by "ba". Note the word "balll" -- true lookbehind should have suppressed the first 2 l's but matched the 2nd pair. But by matching the first 2 l's and then ignoring that match as a false positive, the regexp engine proceeds from the end of that match, and ignores any characters within the false positive.

提交回复
热议问题