简体   繁体   中英

Numbers and Spaces - Loose Phone Regex

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

ie

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

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

就像是:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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