A-HA💡/JS

[정규표현식] \s 와 \s+, 그리고 \S 의 차이점

탱 'ㅅ' 2023. 9. 19. 16:03

 

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.

 

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

 

ref.

https://forum.freecodecamp.org/t/invert-regular-expression-matches-with-javascript-s-vs-s/187456

 

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

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

forum.freecodecamp.org