Regular Expressions

Hello, can someone articulate to me why an inverted regular expression /\S/g doesn’t need to have “+” appended to it but in order to count non-whitespaces but the opposite does? Ex. /\s+/g

/\S/ matches a single character other than white space.
/\s/ Matches a single white space character, including space, tab, form feed, line feed.
+ Matches the preceding expression 1 or more times.

I agree it’s confusing but here’s an example:

so

var str = "Seul les saisons Sont salees"; 

var newstr = str.replace(/\s+/g, "7");            // Seul7les7saisons7Sont7salees
var secstr = str.replace(/\S+/g, "7");               // 7 7 7 7 7
var lstr = str.replace(/\S/g, "7");             // 7777 777 7777777 7777 777777```
1 Like