Javascript – Help with regexp replacing every second comma in the string

javascriptregex

I have a string of that displays like this….

1235, 3, 1343, 5, 1234, 1

I need to replace every second comma with a semicolon

i.e.

1235, 3; 1343, 5; 1234, 1

the string length will always be different but will follow the same pattern as the above i.e. digits comma space digits comma space, etc.

how can I do this with javascript? Is it possible?

Thanks,
Mike

Best Solution

'1235, 3, 1343, 5, 1234, 1'.replace(/([0-9]+),\s([0-9]+),\s/g, '$1, $2; ')