Match everything except for specified strings

前端 未结 7 1663
名媛妹妹
名媛妹妹 2020-11-22 14:25

I know that the following regex will match \"red\", \"green\", or \"blue\".

red|green|blue

Is there a straightforward way of making it mat

相关标签:
7条回答
  • 2020-11-22 14:56

    You don't need negative lookahead. There is working example:

    /([\s\S]*?)(red|green|blue|)/g
    

    Description:

    • [\s\S] - match any character
    • * - match from 0 to unlimited from previous group
    • ? - match as less as possible
    • (red|green|blue|) - match one of this words or nothing
    • g - repeat pattern

    Example:

    whiteredwhiteredgreenbluewhiteredgreenbluewhiteredgreenbluewhiteredgreenbluewhiteredgreenbluewhiteredgreenbluewhiteredgreenbluewhiteredwhiteredwhiteredwhiteredwhiteredwhiteredgreenbluewhiteredwhiteredwhiteredwhiteredwhiteredredgreenredgreenredgreenredgreenredgreenbluewhiteredbluewhiteredbluewhiteredbluewhiteredbluewhiteredwhite

    Will be:

    whitewhitewhitewhitewhitewhitewhitewhitewhitewhitewhitewhitewhitewhitewhitewhitewhitewhitewhitewhitewhitewhitewhitewhitewhite

    Test it: regex101.com

    0 讨论(0)
提交回复
热议问题