Simple parenthesis removal - JavaScript
I would do this by iterating through the tokens and keeping a stack of the current state (signStack) as parenthesis are opened.



function newSign(current, prev) {
  //treat blank as positive
  const currentNegative = current[0] == '-';
  const prevNegative    = prev[0]    == '-';
  return currentNegative == prevNegative ? '+' : '-';
}

function solve(input) {
  let signStack   = ['+']; // start by assuming positive
  let currentSign = '';
  
  const result = input.replace(/(\+|\-)\(?|\)|\w/g, (match) =&gt; {
    if (match==')') { // close parenthesis
      signStack.shift(); 
      currentSign = signStack[0];
      return '';
    } 
    
    if (match[0] != '+' &amp;&amp; match[0] != '-') { // variable
      const  sign = currentSign;
      currentSign = signStack[0];;
      return sign + match;
    }
    
    const sign     = newSign(match, currentSign);
    currentSign    = sign;
    if (match.length &gt; 1) {// open parenthesis
      signStack.unshift(sign);
    }
    return '';
  });  
  
  if (signStack.length != 1)
    throw 'unbalanced parenthesis';
    
  return result;  
}

function test(input, expected) {
  const result = solve(input);
  if (result != expected)
    console.log(`Failed: input=${input}, result=${result}, expected=${expected}`); 
}

test("x-(y+z)",        "x-y-z");
test("x-(y-z)",        "x-y+z");
test("u-(v-w-(x+y))-z", "u-v+w+x+y-z");
test("x-(-y-z)",        "x+y+z");