正则表达式不允许连续的点字符等

我正在尝试制作一个满足以下条件的 JavaScript 正则表达式

  1. az 是可能的

  2. 0-9 是可能的

  3. 破折号、下划线、撇号、句号都是可能的

  4. 和号、括号、逗号、加号是不可能的

  5. 连续期间是不可能的

  6. 句点不能位于开头和结尾

  7. 最多 64 个字符

到目前为止,我已经开始关注正则表达式

^[^.][a-zA-Z0-9-_\.']+[^.]$

但是,这允许中间有连续的点字符并且不检查长度。谁能指导我如何添加这两个条件?


慕妹3242003
浏览 961回答 3
3回答

精慕HU

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

慕村225694

我的想法来了。使用\w(短的字字符)。^(?!.{65})[\w'-]+(?:\.[\w'-]+)*$^在开始 (?!.{65}) 向前看不超过64个字符随后是[\w'-]+一个或多个[a-zA-Z0-9_'-]后跟包含句点的(?:\.?[\w'-]+)* 任意数量的非捕获组,.后跟一个或多个[a-zA-Z0-9_'-]直到$结束以及regex101 上的演示用于尝试

杨魅力

这是一个似乎有效的模式:^(?!.*\.\.)[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
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript