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
'A-HA💡 > JS' 카테고리의 다른 글
[JS/알고리즘] 문자↔︎숫자 변환 (아스키코드)⭐️⭐️ (0) | 2023.09.26 |
---|---|
[JS/알고리즘] 함수 vs. 메소드 차이 (0) | 2023.09.25 |
[JS/알고리즘] 배열에서 값 삭제하는 6가지 방법⭐️ (0) | 2023.09.25 |
[JS/알고리즘] 배열 요소 개행(줄바꿈) 없이 출력하는 방법⭐️⭐️ (0) | 2023.09.21 |
[JS/알고리즘] (2차원) 배열 길이와 값 지정하여 선언하기⭐️ (0) | 2023.09.21 |