开发者

Numbers and Spaces - Loose Phone Regex

开发者 https://www.devze.com 2023-02-22 05:19 出处:网络
How do I test whether a string contains only numbers and spaces using RegEx? i.e. 021 123 4567 is valid

How do I test whether a string contains only numbers and spaces using RegEx?

i.e.

  1. 021 123 4567 is valid
  2. 0211234567 is valid
  3. 0211 234 567 is valid
开发者_Python百科

Pretty loose, but this meets my requirements.

Suggestions?


How about /^[\d ]+$/? If it needs to start and end with a digit, /^\d[\d ]*\d$/ should do it.


  • [0-9] <-- this means any single digit between 0 and 9
  • [0-9 ] <-- this means any single digit between 0 and 9, or a space
  • [0-9 ]{1,} <-- this means any single digit between 0 and 9, or a space, 1 or more times
  • [0-9 ]{1,9} <-- this means any single digit between 0 and 9, or a space, 1 to 9 times


n of digits with any number of spaces:

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

here n=11; Test: here


Something like:

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

精彩评论

暂无评论...
验证码 换一张
取 消