简体   繁体   中英

Using a regular expression in JavaScript, how do I group my matches?

The following code:

 console.log("WatchGuard Technologies (SophosUK) - NDAM - 2003Jan29".match(/[\\W]/gi));
 .as-console-wrapper { max-height: 100% !important; top: 0; }

Produces

[ ' ', ' ', '(', ')', ' ', '-', ' ', ' ', '-', ' ' ]

How do I modify my regular expression so that all the characters between the alpha-numeric characters are grouped together? In other words, I would like the output to be:

[ ' ', ' (',  ') - ', ' - ']

Allow matching more than one character per match with the + quantifier:

.match(/\W+/g)

 console.log( "WatchGuard Technologies (SophosUK) - NDAM - 2003Jan29".match(/\\W+/g) );

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