Regular expression limit string size

后端 未结 2 373
死守一世寂寞
死守一世寂寞 2020-11-28 11:52

How to limit string size for this regular expression?

/^[a-z][a-z0-9]*(?:_[a-z0-9]+)*$/

I just need to add the quantifier {3,16}

相关标签:
2条回答
  • 2020-11-28 12:35

    Sprinkle in some positive lookahead to test for the total length of the string by adding

    (?=.{3,16}$)
    

    at the start of the regex. The final regex is then:

    /^(?=.{3,16}$)[a-z][a-z0-9]*(?:_[a-z0-9]+)*$/
    
    0 讨论(0)
  • 2020-11-28 12:36

    Use regex

    /^[a-z](?:[a-z\d]|_(?!_)){1,14}[a-z\d]$/

    or

    /^(?=.{3,16}$)[a-z][a-z\d]*(?:_[a-z\d]+)*$/

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