Invert Regular Expression Matches with JavaScript_doubt

Tell us what’s happening:

Y didn’t we use + operator here /\S/g, can anyone explain?

Your code so far

// Setup
var testString = "How many non-space characters are there in this sentence?";

// Only change code below this line.

var expression = /\S/g;  // Change this line

// Only change code above this line

// This code counts the matches of expression in testString
var nonSpaceCount = testString.match(expression).length;

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36.

Link to the challenge:

If you used /\S+/g, it will match words, not characters. Appending a + will collect as many contiguous non-space characters in one chunk as possible

1 Like