Suppose I have (in a javascript regular expression)

```
((((A)B)C)D)

```

Of course that really reads

```
ABCD

```

Is there an algorithm to eliminate unnecessary parentheses in a string like that?