简体   繁体   中英

How to use JavaScript regex to find the first sub string until a space?

^([\\w\\-]+) can find the first word but it stops the sub string at a . or _. I only want it to stop at the first space.

You may use \\S+ to match one or more non-space characters. So the regeex must be,

^\S+

Example:

> 'foo.+() bar'.match(/^\S+/)[0]
'foo.+()'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM