Invert Regular Expression Matches with JavaScript \S vs \S+

Invert Regular Expression Matches with JavaScript \S vs \S+
0.0 0

#1

Hi everyone,

I’m having some trouble understanding this challenge, so hoping someone can explain it.

In the previous challenge, to select all the whitespace characters we used \s+

Now to select non-whitespace characters the correct expression is \S

What I don’t understand is why we need the “+” for the first expression but not the second. Why does \S match a different number of characters to \S+?

Thanks for your help!

Link to the challenge:


#2

The difference between \s and \S is that the former matches all the whitespace while the latter matches all nonwhitespace.

The addition of + says to the regex engine - “I want 1 or more of the previous symbol”

Matches involving + are said to be greedy and take as many characters as they can in a given match.

so \s+ says match if there’s one or more whitespace in a row, making the longest match you can each time.

and \S+ says match if there’s one or more nonwhitespace in a row, making the longest match you can each time.

The latter then matches every word rather than every letter in the testcase, and the number of matches due to the greediness is lower - and thus not what the question asks for (it counts words instead of characters)

Does this help?


#3

Yeah that makes way more sense now, thanks.

So just to make sure that I got it, say I had two whitespace characters in a row and I wanted to select them:

\s would make two selections, both one whitespace character long
\s+ would make one selection that is two characters long

Is that correct?


#4

That’s correct!

Your question gets asked a lot, so I wonder if it needs a better explanation on the questions themselves


#5

Wonderful!

Yeah, it certainly wasn’t clear to me before, and if that’s true for other people too, maybe it should be updated.

Thanks a lot!