一段文字 如
[aaa](bbb(ASDASD)XSSAXASX)
我想获取的内容是bbb(ASDASD)XSSAXASX
function nestMatch($str)
{
let map = {
'text' : []
,'match' : []
};
$str.replace(/(\(|\))([^\(\)]*)/g, function($match, $tag, $text){
let text = '';
switch($tag)
{
case '(':
map.text.unshift($text);
break;
case ')':
if(map.text.length > 0)
{
text = '(' + map.text.shift() + ')';
if(map.text.length <= 0)
map.text[0] = '';
map.text[0] = map.text[0] + text + $text;
map.match.push(text);
}
break;
}
return $match;
});
return map.match;
}
console.log(nestMatch('111(aaa(bbb)ccc)222(ddd(eee)fff(ggg)hhh)333'));
输出 [ "(bbb)", "(aaa(bbb)ccc)", "(eee)", "(ggg)", "(ddd(eee)fff(ggg)hhh)" ]
10 回答11.3k 阅读
5 回答4.9k 阅读✓ 已解决
4 回答3.2k 阅读✓ 已解决
2 回答2.8k 阅读✓ 已解决
3 回答2.4k 阅读✓ 已解决
3 回答2.2k 阅读✓ 已解决
2 回答2.6k 阅读✓ 已解决
试下这个看看是不是你想要的结果