They both seem to work but I have been told you should use both when you're forming a RegExp?
-
1i think this is a general regex question. maybe take out javascript,html tags and change the title. – ddavison Jul 30 '13 at 15:18
-
\s denotes whitespace and where as \t denotes tab – Shushant Jul 30 '13 at 15:26
-
1Just a pedantic adjustment to what most answers are saying here: `[\s\t]` is redundant. The `\t` is already part of `\s` so you don't have to include the `\t`. In the case of `\s\t`, the `\t` is not redundant. It is looking for a whitespace, followed by a tab. So be careful if you're dealing with a character class or not. – Shaz Jul 30 '13 at 15:41
4 Answers
\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.
- 258,201
- 41
- 486
- 479
-
2
-
2
-
1This answer has been added to the [Stack Overflow Regular Expression FAQ](http://stackoverflow.com/a/22944075/2736496), under "Escape Sequences". – aliteralmind Apr 10 '14 at 01:02
\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
- 7,822
- 4
- 31
- 52
\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.
- 127,459
- 24
- 238
- 287
\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.
- 666
- 4
- 14