简体   繁体   English

在 JavaScript 中使用正则表达式,如何对匹配项进行分组?

[英]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) );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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