开发者

JavaScript special characters/regular expressions

开发者 https://www.devze.com 2023-02-17 06:07 出处:网络
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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号