Evil Washing Machine
Evil Washing Machine

Reputation: 1341

What is the difference between \s and \t?

They both seem to work but I have been told you should use both when you're forming a RegExp?

Upvotes: 10

Views: 34577

Answers (4)

arshajii
arshajii

Reputation: 129537

\t is a literal tab whereas \s is a predefined character class. \s matches any whitespace character while \t matches only tabs (which are also matched by \s).

This is similar to asking what the difference between \d and 0 is. 0 is a literal 0 whereas \d is any digit.

Upvotes: 3

Song Gao
Song Gao

Reputation: 666

\s contains all whitespace characters. For example, in Java, \s is [\t\n\x0b\r\f]. \t is just a single tab, so you don't need to use both.

Upvotes: 0

talemyn
talemyn

Reputation: 7960

\s matches a single whitespace character, which includes spaces, tabs, form feeds, line feeds and other unicode spaces.

\t Matches a single tab.

If you are using \s, you don't need to include \t.

More information on regex patterns here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp

Upvotes: 13

Frédéric Hamidi
Frédéric Hamidi

Reputation: 263047

\s matches any whitespace character, including tabs. \t only matches a tab character.

\t being a subset of \s, you should not have to use both at the same time.

Upvotes: 18

Related Questions