正则表达式禁止连续超过 1 个破折号 [英] Regex to disallow more than 1 dash consecutively

查看:51
本文介绍了正则表达式禁止连续超过 1 个破折号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  1. 如何禁止 --(连续 1 个以上 -)?例如ab--c
  2. - 在单词后面不允许,例如abc-
  3. - 在词的开头不允许,例如-abc
  1. How can I disallow -- (more than 1 consecutive -)? e.g. ab--c
  2. - at the back of words not allow, e.g. abc-
  3. - at start of words not allow, e.g. -abc

^[A-Za-z0-9-]+$ 是我目前所拥有的.

推荐答案

^(?!-)(?!.*--)[A-Za-z0-9-]+(?<!-)$

说明:

^             # Anchor at start of string
(?!-)         # Assert that the first character isn't a -
(?!.*--)      # Assert that there are no -- present anywhere
[A-Za-z0-9-]+ # Match one or more allowed characters
(?<!-)        # Assert that the last one isn't a -
$             # Anchor at end of string

这篇关于正则表达式禁止连续超过 1 个破折号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆