Search code examples
regexregex-negation

Apply negative look-ahead to preceeding group


I'm trying since hours to get this negative-look-ahead to work for me. It should match my string only if it's NOT followed by '/CCC'

/(^[\w]+)(?!./CCC$)/mg

Test string:

BBB/CCC
AAA/DDD/CCC

Could someone point out why my pattern still matches the 'BBB' of the first line?


Solution

  • Firstly, you have to escape the / inside the regular expression.

    You also have a dot that shouldn't be there and are missing a word boundary:

    /(^\w+)\b(?!\/CCC$)/mg