\s: every whitespace character it actually matches
\s matches the six ASCII whitespace characters plus the no break space, the byte order mark and the Unicode space separators. It does not match the zero width space.
At a glance
- Syntax
- \s
- ASCII members
- space, tab, line feed, vertical tab, form feed, carriage return
- Also includes
- no break space U+00A0, byte order mark U+FEFF, and the Zs category
- Excludes
- zero width space U+200B, which is not a space separator
- Negated form
- \S
- Horizontal only
- [ \t], because JavaScript has no \h shorthand
In detail
The list is longer than most people expect. Alongside the obvious space, tab and the three line terminators, \s covers the vertical tab and form feed, the no break space that word processors and content management systems insert, the byte order mark, and the whole Unicode space separator category including the ideographic space used in Japanese text.
That breadth is usually a feature. A trim built on /^\s+|\s+$/g removes a stray no break space pasted in from a document, where a hand written class of space and tab would leave it and the value would keep failing an equality check for no visible reason.
The one it skips matters just as much. The zero width space, U+200B, is not a space separator in Unicode, so \s does not match it. It is invisible, it survives a trim, and it is a common cause of a string that looks identical to another one and compares unequal. Finding it takes an explicit /\u200b/ or a character code dump.
The other thing \s does not give you is a horizontal only option. There is no \h in JavaScript, so trimming the ends of each line without eating the blank lines between them means writing [ \t] out by hand. That distinction is the difference between tidying a file and collapsing it.
Examples
/\s/.test("\u00a0"); // true, no break space
/\s/.test("\ufeff"); // true, byte order mark
/\s/.test("\u3000"); // true, ideographic space
/\s/.test("\u200b"); // false, zero width space"a b\tc".split(/\s+/); // ["a", "b", "c"]
// Horizontal whitespace only, which \s cannot express on its own.
/^[ \t]+/.exec("\t code")[0].length; // 3Things that catch people out
- The zero width space U+200B is not matched by \s, so it survives every trim and collapse and leaves two visually identical strings comparing unequal.
- \s includes line terminators, so /\s+/ used to collapse spacing also flattens paragraph breaks. Use [ \t]+ when the line structure has to survive.
- Splitting a tab separated row on /\s+/ silently merges consecutive tabs and loses every empty field. Split on /\t/ instead.
Try it: Regex Tester
Type a pattern and a test string, watch every match highlight as you go, read a plain English breakdown of each token, and check the numbered and named groups it captured.
Regex Tester
LOCAL · Browser-OnlyTest and debug regular expressions with live matching and highlights
Related Regex syntax
All regex syntax →\S: non whitespace, and the [\s\S] any character trick
\S is the complement of \s, which makes it a blank check in one character. Paired with \s it also gives the standard match anything class.
Regex to trim whitespace, collapse runs and drop blank lines
There is no preset for this one. Four short patterns cover trimming, collapsing, per line trimming and removing blank lines, and each has a different failure.
\t: matching tabs in TSV and indented text
\t matches the horizontal tab. Splitting a tab separated row on \s+ instead destroys every empty field, and JavaScript has no \h shorthand.
The dot: any character except a line terminator
What the dot matches by default, the four characters it skips, how the u and s flags change it, and why it needs no escape inside a character class.
[abc]: character sets and what loses its meaning inside
A bracket set matches one character from a list. Most metacharacters go literal inside it, and only four keep any special meaning at all.
[^abc]: negated character sets that quietly cross newlines
A negated class matches one character that is not in the set, including line terminators. That is why a quoted string pattern runs across lines.
Tools for this
- Regex Tester Free online regex tester - test and debug regular expressions with live matching and highlights
- Regex Playground Regex playground with live match highlighting, named groups, find and replace, and shareable links