John Threepwood
John Threepwood

Reputation: 16143

How to check if a string contains a pattern separated by whitespace?

How to check if a string contains a pattern separated by whitespace?

Examples:

"abc  ef    ds       ab  "

Now I would like to check if the given string consists only of the pattern [a-z] separated by whitespace. My try: ^\s*[a-z]*\s*$. But this checks only whitespace in the beginning and end, not if the whitespaces is used for separation of the content.

Upvotes: 0

Views: 330

Answers (2)

Jon Hanna
Jon Hanna

Reputation: 113282

^(\s|[a-z])*$

Zero or more case characters that are either whitespace, or A-Z.

If you want to make sure there's at least one thing other than white space, then:

^\s*[a-z]+(\s*|[a-z])*$

Zero or more whitespace, at least one character A-Z, then the same as above.

Upvotes: 1

Aziz Shaikh
Aziz Shaikh

Reputation: 16524

Try this regular expression:

/^[a-z\s]+$/

Upvotes: 3

Related Questions