我试图解析以下字符串格式:
<id>:<name>[,<name>]*
例如,以123:test,south-west,best,rest_well
为例。
我编写了以下regex:
/(\d+):([a-zA-Z0-9_\-]+)(?:,([a-zA-Z0-9_\-]+))*/
我的假设是,(?:,([a-zA-Z0-9_\-]+))
将捕获可选的附加名称(西南、最佳和rest_well)。但是,它只捕获姓氏'rest_well‘。
打印出来的火柴:
'123:test,south-west,best,rest_well'.match(/(\d+):([a-zA-Z0-9_\-]+)(?:,([a-zA-Z0-9_\-]+))*/);
> ["123:test,south-west,best,rest_well", "123", "test", "rest_well"]
我期待的是:
> ["123:test,south-west,best,rest_well", "123", "test", "south-west", "best", "rest_well"]
我相信其他语言实际上会积累匹配的组,但不知怎么的,这是失败的。也许我错过了一个小细节。任何帮助都是非常感谢的!
发布于 2014-06-20 17:15:44
听起来你只是想用:
或,
来分割字符串。这个能行吗?
var str = "123:test,south-west,best,rest_well";
var res = str.split(/:|,/);
输出
["123", "test", "south-west", "best", "rest_well"]
https://stackoverflow.com/questions/24332322
复制相似问题