I sometimes want to match whitespace but not newline.
So far I\'ve been resorting to [ \\t]
. Is there a less awkward way?
The below regex would match white spaces but not of a new line character.
(?:(?!\n)\s)
DEMO
If you want to add carriage return also then add \r
with the |
operator inside the negative lookahead.
(?:(?![\n\r])\s)
DEMO
Add +
after the non-capturing group to match one or more white spaces.
(?:(?![\n\r])\s)+
DEMO
I don't know why you people failed to mention the POSIX character class [[:blank:]]
which matches any horizontal whitespaces (spaces and tabs). This POSIX chracter class would work on BRE(Basic REgular Expressions), ERE(Extended Regular Expression), PCRE(Perl Compatible Regular Expression).
DEMO