Skip to content Skip to sidebar Skip to footer

Replace Content Present In The Nested Brackets

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, '

Solution 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;
varInput = "ABCDEF ((3) abcdef),GHIJKLMN ((4)(5) Value),OPQRSTUVW((4(5)) Value (3))";
varOutput = "";
for (var i=0; i < Input.length; i++) {
    if (Input[i] === '(') {
        pCount++;
    }
    elseif (Input[i] === ')') {
        pCount--;
    }
    elseif (pCount == 0) {
        Output += Input[i];
    }
}

Output = Output.replace(/ /g,'');
console.log(Output);

Solution 2:

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

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

Or, you could use a recursive function:

functionremove_nested_parens(s) {
    let new_s = s.replace(/\s*\([^()]*\)/g, "");
    return new_s == s ? s : remove_nested_parens(new_s);
}
console.log(remove_nested_parens("ABCDEF ((3) abcdef),GHIJKLMN ((4)(5) Value),OPQRSTUVW((4(5)) Value (3))"));

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.

Post a Comment for "Replace Content Present In The Nested Brackets"