javascript and php to change string by specific rule [closed]

how to let

a1(b)w; b5(a)w;

become

a1w;abw;b5w;baw;

using javascript and php?

which means that:

  1. remove parenthesis.
  2. generate a string without parenthesis and its content.
  3. generate another string with the content in parenthesis and remove one character before the “(“

I tried to split the string first but it’s too complicated.