Using a text editor, without writing JavaScript functions (e.g. can't use this great answer: [Replacing the nth instance of a regex match in Javascript](https://stackoverflow.com/questions/36183/replacing-the-nth-instance-of-a-regex-match-in-javascript)) 

How do I take something like this (a proprietary code structure I have no control over)

```
map(key1, value1, key2, value2, key3, value3, key4, value4 ...)

```

And convert it to this 

```
map(
  key1, value1,
  key2, value2,
  key3, value3,
  key4, value4 
  ...
)

```

One option I found was using this regex to find every 2nd comma `(([^,]*,){2})`

Then replace with `\1\n\t`

But I would like to improve it:

**1. It doen't handle the first and last lines very well**

```
 map(key1, value1,
    key2, value2,
    key3, value3,
    key4, value4 ...)

```

**2. Only works on a flat structure**

e.g. I can't think of a way to transform this 

```
map(key1, value1, key2, map(key3, value3, key4, value4 ...), key5, value5)

```

To this 

```
map(
  key1,value1,
  key2,map(
    key3,value3,
    key4,value4 
  ),
  key5,value5    
)

```

Using regex, or is there a way?