Invert Regular Expression Matches help please

Can someone explain why to match whitespaces I need to use \s+, but to match anything non-whitespace I use \S. Without the + sign?
Thank you

1 Like

The + (plus) at the end means the character before it can appear one or more times. In your question, \s matches one or more whitespace characters (space, tab, return). And the \S matches everything that is not whitespace. It doesn’t mean that plus should only be used for whitespace characters. It just depends on what you’re targeting. If you want to match multiple non-whitespace characters, you can use a + at the end of \S too.

Thank you! makes sense

I’m still not a 100% clear on this.

If we use testString.match(expression), whereby expression is
(1) /\S/g
(2) /\S+/g
(3) /\s/g
(4) /\s+/g

What are we matching here?

My assumption goes as follows:
(1) non-whitespace character
(2) all non-whitespace characters
(3) whitespace character
(4) all whitespace characters

Assuming testString = “This is a test\n”:
(1) T
(2) Thisisatest
(3) " "
(4) " ", " ", " ", “\n”

What am I missing here?

1 . /\S/g
every single nonwhitespace character separately

2 . /\S+/g
all sequences of contiguous nonwhitespace characters i.e. words

3 . /\s/g
every single whitespace character separately

4 . /\s+/g
all sequences of contiguous whitespace characters i.e. runs of whitespace

the string match function returns an array of matches for a regex with the global g flag - this is extremely easy to test in the console - here’s what you’ll get

s="This is        a        test\n"
console.log(`s "${s}"`)
re=/\S/g
m=s.match(re)
console.log(`re ${re} m [${m}]`)
re=/\S+/g
m=s.match(re)
console.log(`re ${re} m [${m}]`)
re=/\s/g
m=s.match(re)
console.log(`re ${re} m [${m}]`)
re=/\s+/g
m=s.match(re)
console.log(`re ${re} m [${m}]`)

output

s "This is        a        test
"
re /\S/g m [T,h,i,s,i,s,a,t,e,s,t]
re /\S+/g m [This,is,a,test]
re /\s/g m [ , , , , , , , , , , , , , , , , ,
]
re /\s+/g m [ ,        ,        ,
]

these pages are required reading for anyone learning and using javascript regexes - it can take years or decades to master regexes so don’t feel rushed or frustrated

1 Like