How to get the content in [] ()

A paragraph, such as

[aaa](bbb(ASDASD)XSSAXASX)

what I want to get is bbb (ASDASD) XSSAXASX


javascript regularities cannot do nested matching. If you really want to do so, you can write a function to implement it. In your case, you can use a stack mechanism


[xxxx] (asdasdasd () asdasdasdasd) because it is parsed in md format. What I want is asdasdasd () asdasdasdasd


/(?<=\().*(?=\))/

try this and see if it's the result you want


this problem is described. Others need to guess what the data and results you are dealing with



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'));

output ["(bbb)", "(aaa (bbb) ccc)", "(eee)", "(ggg)", "(ddd (eee) fff (ggg) hhh)]

Menu