验证复杂字符串输入(固定长度模式)

发布于 2025-01-15 10:59:09 字数 137 浏览 3 评论 0原文

验证此类输入的最佳方法是什么:

1234,4589,7889

仅允许的数字。每个数字必须包含 4 位数字。它们由 , 分隔。可以有一个,也可以有多个。最后一项没有分隔符。

What is the best way to validate this type of input:

1234,4589,7889

Only allowed numbers. Each number must contain 4 digits. They are separated by ,. There can be one or many. Last item does not have a separator.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鸵鸟症 2025-01-22 10:59:09
  • 每个数字必须包含 4 位数字:\d{4}
  • 它们之间用“,”分隔:\d{4},\d{4}
  • 可以是一个或许多 : (?:\d{4},)+
  • 最后一项没有分隔符 =>又名 4 位数字后跟任意数量的逗号 + 4 位数字: \d{4}(?:,\d{4})*

^\d{4}(?:,\ d{4})*$ 以确保模式覆盖字符串的开头和结尾。

  • Each number must contain 4 digits : \d{4}
  • They are separated by "," : \d{4},\d{4}
  • There can be one or many : (?:\d{4},)+
  • Last item does not have a separator => aka 4 digits followed by any number of comma + 4 digits : \d{4}(?:,\d{4})*

^\d{4}(?:,\d{4})*$ to make sure the pattern cover the start and the end of the string.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文