How do I test whether a string contains only numbers and spaces using RegEx?
i.e.
Pretty loose, but this meets my requirements.
Suggestions?
How do I allow blank space in regex? \s is the regex character for whitespace. It matches spaces, new lines, carriage returns, and tabs.
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].
\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).
How about /^[\d ]+$/
? If it needs to start and end with a digit, /^\d[\d ]*\d$/
should do it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With