Regex that does not allow consecutive dots

后端 未结 3 1448
我在风中等你
我在风中等你 2020-11-29 13:18

I have a Regex to allow alphanumeric, underscore and dots but not consecutive dots:

^(?!.*?[.]{2})[a-zA-Z0-9_.]+$

I also need to now allow

3条回答
  •  野性不改
    2020-11-29 13:54

    You can use it like this with additional lookaheads:

    ^(?!\.)(?!.*\.$)(?!.*?\.\.)[a-zA-Z0-9_.]+$
    
    • (?!\.) - don't allow . at start
    • (?!.*?\.\.) - don't allow 2 consecutive dots
    • (?!.*\.$) - don't allow . at end

提交回复
热议问题