Confusing regular expression in Java Script

In this challenge Find Whitespace with Regular Expressions we have to count all the whitespace characters in the sentence.

One of the requirements is:
“Use the /\s+/g regular expression to find the spaces in testString.”

But when I use: var expression = /\s/g; It finds 7 characters anyway.
So what exactly difference does that + sign makes?

/\s/g will only match individual whitespace characters.
/\s+/g will match “blocks” of whitespace characters.

If you have

var s = 'This is   some   text';

/\s/g will give 7 matches (there are 7 spaces in that string)
/\s+/g will give only 3.


O_O Now I see!

1 Like