\s
, with a lowercase s
. You can also search for everything except whitespace.
Search for non-whitespace using \S
, which is an uppercase s
. This pattern will not match whitespace, carriage return, tab, form feed, and new line characters. You can think of it being similar to the character class [^ \r\t\f\n\v]
.
let whiteSpace = "Whitespace. Whitespace everywhere!"
let nonSpaceRegex = /\S/g;
whiteSpace.match(nonSpaceRegex).length; // Returns 32
countNonWhiteSpace
to look for multiple non-whitespace characters in a string.
\S/code> to match all non-whitespace characters.');
- text: Your regex should find 35 non-spaces in "Men are from Mars and women are from Venus."
testString: assert("Men are from Mars and women are from Venus.".match(countNonWhiteSpace).length == 35, 'Your regex should find 35 non-spaces in "Men are from Mars and women are from Venus."
');
- text: 'Your regex should find 23 non-spaces in "Space: the final frontier."
'
testString: 'assert("Space: the final frontier.".match(countNonWhiteSpace).length == 23, ''Your regex should find 23 non-spaces in "Space: the final frontier."
'');'
- text: Your regex should find 21 non-spaces in "MindYourPersonalSpace"
testString: assert("MindYourPersonalSpace".match(countNonWhiteSpace).length == 21, 'Your regex should find 21 non-spaces in "MindYourPersonalSpace"
');
```