AlphaWolf AlphaWolf - 1 year ago 38
Javascript Question

regex how to remove commas after a specific word

I am currently trying to remove any commas after the specific word 'SUM(' and Before the last ')' by using a regex. In other words, I want to remove all the beginning and end commas inside of the function SUM(). I am using the javascript function replace to do this on a regex.

Here are examples to illustrate what I am trying to do after the replace function is called with a regex:

SUM(,,,,1,2,) // result should be SUM(1,2)
SUM(,1,2,) // result should be SUM(1,2)
SUM(,1,2,,,) // result should be SUM(1,2)
SUM(,,,,1,34) // result should be SUM(1,34)
SUM(,,,,0,0,0) // result should be SUM(0,0,0)

Currently I have been trying this regex:


But I only want to match on the commas that are after the word 'SUM(' and Before the last bracket ')'. Please can someone help me.


Use this pattern:


And replace it with $1($2).

Online Demo