简体   繁体   English

数字和空间 - 松散的电话正则表达式

[英]Numbers and Spaces - Loose Phone Regex

How do I test whether a string contains only numbers and spaces using RegEx? 如何使用RegEx测试字符串是否仅包含数字和空格?

ie

  1. 021 123 4567 is valid 021 123 4567有效
  2. 0211234567 is valid 0211234567有效
  3. 0211 234 567 is valid 0211 234 567有效

Pretty loose, but this meets my requirements. 相当宽松,但这符合我的要求。

Suggestions? 建议?

How about /^[\\d ]+$/ ? 怎么样/^[\\d ]+$/ If it needs to start and end with a digit, /^\\d[\\d ]*\\d$/ should do it. 如果它需要以数字开头和结尾, /^\\d[\\d ]*\\d$/应该这样做。

  • [0-9] <-- this means any single digit between 0 and 9 [0-9] < - 这意味着0到9之间的任何单个数字
  • [0-9 ] <-- this means any single digit between 0 and 9, or a space [0-9 ] < - 这意味着0到9之间的任何单个数字或空格
  • [0-9 ]{1,} <-- this means any single digit between 0 and 9, or a space, 1 or more times [0-9 ]{1,} < - 这意味着0到9之间的任何单个数字,或1个或更多个空格
  • [0-9 ]{1,9} <-- this means any single digit between 0 and 9, or a space, 1 to 9 times [0-9 ]{1,9} < - 这意味着0到9之间的任何单个数字,或1到9倍的空格

n of digits with any number of spaces: n个具有任意数量空格的数字:

^(\s*\d\s*){11}$

here n=11; 这里n = 11; Test: here 测试: 这里

就像是:

\d*|\s+^[A-Za-z]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM