Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numbers and Spaces - Loose Phone Regex

Tags:

c#

regex

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

Pretty loose, but this meets my requirements.

Suggestions?

like image 521
Marko Avatar asked Apr 04 '11 23:04

Marko


People also ask

Are spaces allowed in regex?

How do I allow blank space in regex? \s is the regex character for whitespace. It matches spaces, new lines, carriage returns, and tabs.

How do you handle space in regex?

The RegExp \s Metacharacter in JavaScript is used to find the whitespace characters. The whitespace character can be a space/tab/new line/vertical character. It is same as [ \t\n\r].

What does \d include in regex?

\d (digit) matches any single digit (same as [0-9] ). The uppercase counterpart \D (non-digit) matches any single character that is not a digit (same as [^0-9] ). \s (space) matches any single whitespace (same as [ \t\n\r\f] , blank, tab, newline, carriage-return and form-feed).


1 Answers

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

like image 60
CanSpice Avatar answered Sep 22 '22 11:09

CanSpice