JavaScript special characters/regular expressions
I'm trying to learn from reading Mozilla documentation for regular expressions, but there's one thing I don't get. For the special character \s
it gives the following example
/\s\w*/ matches ' bar' in "foo 开发者_开发百科bar."
I understand that \s is the special character for white space, but why is there a w* in the example?
doesn't /\s/
also match ' bar' in "foo bar."?
What's with the w*
?
/\s\w*/
is whitespace character followed by 0 or more word characters.
/\s/
would only find the whitespace in the example.
\w matches any alphanumerical character (word characters) including underscore (short for [a-zA-Z0-9_]).
It's a character escape.
\w
is all word characters (letters, digits, and underscores)
Check this link for more documentation on such shorthand
精彩评论