[英]Regex not to allow to consecutive dot characters and more
我正在嘗試制作一個滿足以下條件的JavaScript正則表達式
到現在為止,我來關注正則表達式
^[^.][a-zA-Z0-9-_\.']+[^.]$
但是,這允許在中間連續使用點字符,並且不檢查長度。 誰能指導我如何添加這兩個條件?
這是一個似乎有效的模式:
^(?!.*\.\.)[a-zA-Z0-9_'-](?:[a-zA-Z0-9_'.-]{0,62}[a-zA-Z0-9_'-])?$
這是正則表達式模式的解釋:
^ from the start of the string
(?!.*\.\.) assert that two consecutive dots do not appear anywhere
[a-zA-Z0-9_'-] match an initial character (not dot)
(?: do not capture
[a-zA-Z0-9_'.-]{0,62} match to 62 characters, including dot
[a-zA-Z0-9_'-] ending with a character, excluding dot
)? zero or one time
$ end of the string
您可以使用this
正則表達式
^(?!^[.])(?!.*[.]$)(?!.*[.]{2})[\w.'-]{1,64}$
正則表達式分解
^ #Start of string
(?!^[.]) #Dot should not be in start
(?!.*[.]$) #Dot should not be in start
(?!.*[.]{2}) #No consecutive two dots
[\w.'-]{1,64} #Match with the character set at least one times and at most 64 times.
$ #End of string
正則表達式中的更正
-
不應介於字符類之間。 它表示范圍。 避免在兩者之間使用它 [a-zA-Z0-9_]
等效於\\w
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.