Replace content present in the nested brackets

2019-07-12 17:22发布

问题:

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

Expected Output = ABCDEF,GHIJKLMN,OPQRSTUVW

Tried so far

Output = Input.replace(/ *\([^)]*\)*/g, "");

回答1:

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



回答2:

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.