Alex Rivera | Logout

Regex: don't match if string contains whitespace

Asked 2010-01-07T04:10:33.087
11

I can't seem to figure out the regex pattern for matching strings only if it doesn't contain whitespace. For example

"this has whitespace".match(/some_pattern/)

should return nil but

"nowhitespace".match(/some_pattern/)

should return the MatchData with the entire string. Can anyone suggest a solution for the above?

Edit
Report

1 Answer

3

Not sure you can do it in one pattern, but you can do something like:

"string".match(/pattern/) unless "string".match(/\s/)
answered 2010-01-07T04:19:34.423

Your Answer