Replace content present in the nested brackets

前端 未结 2 1675
佛祖请我去吃肉
佛祖请我去吃肉 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);

提交回复
热议问题