What Does the Regex \S Mean in Javascript? [Duplicate]

What Does the Regex \S Mean in Javascript? [Duplicate]

What does /\S/ mean in a regex?

while (cur ! = null) {
    if (cur.nodeType == 3 && ! /\S/. test(cur.nodeValue)) {
        element. removeChild(cur);
    } else if (cur. nodeType == 1) {
        cleanWhitespace(cur);
    }
}
3

5 Answers

\s matches whitespace (spaces, tabs and new lines). \S is negated \s.

\S matches anything but a whitespace, according to this reference.

3

I believe it means 'anything but a whitespace character'.

/\S/.test(string) returns true if and only if there's a non-space character in string. Tab and newline count as spaces.

The \s metacharacter matches whitespace characters.

2
David Miller
Author

David Miller

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.