Regular expression to match A, AB, ABC, but not AC. (“starts with”)

后端 未结 4 1319
被撕碎了的回忆
被撕碎了的回忆 2020-12-02 00:40

I\'m banging my head against a wall. I want a regex that matches: empty string, A, AB, and ABC, but not AC. I have this,

4条回答
  •  情深已故
    2020-12-02 01:26

    /^A(?:B(?:C)?)?$/
    

    should do it.

    This is using the non-capturing group construct (?: xxx ) so as not to mess up any match capturing you may be doing.

提交回复
热议问题