Regex not to allow double underscores

后端 未结 3 852
你的背包
你的背包 2021-01-19 01:58

Trying to apply regex for not allowing a string with double underscores

 [a-z][a-z0-9_-]+[^__]

but its failing in many cases like:

3条回答
  •  时光取名叫无心
    2021-01-19 02:02

    [^__] matches one character that is not underscore. To assert that your string doesn't have two consecutive underscores, you could use a negative lookahead:

    ^(?!.*__.*)[a-z][a-z0-9_-]+$
    

    The lookaround asserts that your string does not have two consecutive underscores (?!.*__.*), and then matches your required string if the assertion does not fail -- [a-z][a-z0-9_-]+.

提交回复
热议问题