簡體   English   中英

正則表達式不允許連續的點字符和更多

[英]Regex not to allow to consecutive dot characters and more

我正在嘗試制作一個滿足以下條件的JavaScript正則表達式

  1. az是可能的
  2. 0-9是可能的
  3. 破折號,下划線,撇號,句點是可能的
  4. 不能使用&號,括號,逗號和加號
  5. 連續的時期是不可能的
  6. 時間段不能位於開始和結束位置
  7. 最多64個字符

到現在為止,我來關注正則表達式

^[^.][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

我的想法來了。 使用\\w單詞字符的 縮寫 )。

^(?!.{65})[\w'-]+(?:\.[\w'-]+)*$
  • 開頭的 ^ (?!.{65}) 前面的字符數不能超過64個
  • 隨后是[\\w'-]+ [a-zA-Z0-9_'-] [\\w'-]+ [a-zA-Z0-9_'-]一個或多個
  • 后跟(?:\\.?[\\w'-]+)* 任意數量非捕獲組,其中包含一個句點. 跟一個或多個[a-zA-Z0-9_'-]直到$ end

regex101上演示

您可以使用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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM