var results = [];
s = s.replace(/\[+(?:(\w+):)?(.*?)\]+/g,
function(g0, g1, g2){
results.push([g1, g2.split(',')]);
return "";
});
给出结果:
>> results =
[["this", [" is", " how"]],
["it", [" works", " but", " there"]],
["", ["might be bracket", " parts", " without", " colons "]],
["", ["nested sections should be ignored?"]]
]
>> s = "hi, i'm a string "
请注意,它在标记之间留有空格。另外,您还可以删除[[]]
通过调用在早期阶段获取令牌s = s.replace(/\[\[.*?\]\]/g, '');
- 此代码将它们捕获为正常组。