Replace content present in the nested brackets

前端 未结 2 1670
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-21 07:20

Input = ABCDEF ((3) abcdef),GHIJKLMN ((4)(5) Value),OPQRSTUVW((4(5)) Value (3))

Expected Output = ABCDEF,GHIJKLMN,OPQRSTUVW

Tried

相关标签:
2条回答
  • 2020-12-21 08:10

    Using a regex here probably won't work, or scale, because you expect nested parentheses in your input string. Regex works well when there is a known and fixed structure to the input. Instead, I would recommend that you approach this using a parser. In the code below, I iterate over the input string, one character at at time, and I use a counter to keep track of how many open parentheses there are. If we are inside a parenthesis term, then we don't record those characters. I also have one simple replacement at the end to remove whitespace, which is an additional step which your output implies, but you never explicitly mentioned.

    var pCount = 0;
    var Input = "ABCDEF ((3) abcdef),GHIJKLMN ((4)(5) Value),OPQRSTUVW((4(5)) Value (3))";
    var Output = "";
    for (var i=0; i < Input.length; i++) {
        if (Input[i] === '(') {
            pCount++;
        }
        else if (Input[i] === ')') {
            pCount--;
        }
        else if (pCount == 0) {
            Output += Input[i];
        }
    }
    
    Output = Output.replace(/ /g,'');
    console.log(Output);

    0 讨论(0)
  • 2020-12-21 08:10

    If you need to remove nested parentheses, you may use a trick from Remove Nested Patterns with One Line of JavaScript.

    var Input = "ABCDEF ((3) abcdef),GHIJKLMN ((4)(5) Value),OPQRSTUVW((4(5)) Value (3))";
    var Output = Input;
    while (Output != (Output = Output.replace(/\s*\([^()]*\)/g, ""))); 
    console.log(Output); 

    Here, \s*\([^()]*\) matches 0+ whitespaces, (, 0+ chars other than ( and ) and then a ), and the replace operation is repeated until the string does not change.

    0 讨论(0)
提交回复
热议问题