TheLovelySausage
TheLovelySausage

Reputation: 4124

RegEx, Exclude All Whitespace Except Space

I have a simple RegEx that is being used to match all Non-Whitespace Characters

([^\s])

Which is fine but the only issue is that it is not matching on valid Whitespace (valid in my case) which is just a regular space

How can I add an exception to this exclusion for regular space? Match all Non-Whitespace Characters but also match for Space

Upvotes: 0

Views: 2445

Answers (1)

Chase
Chase

Reputation: 5625

You should use the regex - [^\t\n\r\f\v]

i.e manually exclude all the whitespace except for space.

Check out the demo here

Edit: As mentioned in the comments, \s contains more than just \t\n\r. Although \t\n\r are the common ones, it's not the end of the story, far from it! I've added those into the regex as well.

However, thanks to Wiktor, the preferred, and easier, answer should be- [\S ]

This will include only non-whitespace characters and space.

Check out the demo here

Upvotes: 1

Related Questions