patad
patad

Reputation: 9672

Regexp: Check for digits and whitespaces

I am using the following regexp to make sure my string contains 5 digits. It works perfect, however, I also need to allow whitespaces: 123 45 and 12345 need to pass the test.

string.match(/^\d{5}$/g);

Thanks

Upvotes: 0

Views: 184

Answers (1)

Gumbo
Gumbo

Reputation: 655129

You could simply remove any whitespace and then try to match the rest:

string.replace(/\s/g, "").match(/^\d{5}$/g)

Upvotes: 3

Related Questions