Regular matching to get characters

string {4} A (- 1) {2} (A) + {2} (B (Tan (C)-Tan (D)} A (2) , how does
write regular matching to get the content of {} () ?
for example, I want to get {2} root (A) and {2} root (B (Tan (C)-Tan (D) ) in the above string.

Mar.02,2021

js regular currently does not support multi-level recursion, so you cannot find the case of multi-level nesting

regular match multiple layers of parentheses?

take your question as an example, you can find } square (, then { on the left, on the right) . There is no simplified content in the following code, so it should be easy to understand.

String.prototype._match = function(a,b,c){
    console.log(a,b,c);//abca/\}\(/g
    let str = this.toString();
    let arr = [];
    str.replace(/\}\(/g,function(item,i){
        let flag1=1,flag2=1;
        for(var m=i-1;m>=0;m--){
            if(str[m]=="}"){
                flag1PP
            }else if(str[m]=="{"){
                flag1--
            }
            if(flag1==0){
                break;
            }
        }
        for(var n=i+3;n<str.length;nPP){
            if(str[n]=="("){
                flag2PP
            }else if(str[n]==")"){
                flag2--
            }
            if(flag2==0){
                break;
            }
        }
        flag1 || flag2 || arr.push(str.substring(m,n+1));//flag1 flag2true
    })
    return arr;
}
console.log('{4}(-1){{2}(A)+{2}(B(Tan(C)-Tan(D)))}(2)'._match())
console.log('{4}(-1){{2}(A)+{{4}(-1)}(B(Tan(C)-Tan(D)))}(2)'._match())
Menu