var str = 'aa (xx, dd, ee), f gg (hh, dd, pp)'; x = str.split(/\(|\)/g); for (var i = 1; i < x.length; i += 2) x[i] = '(' + x[i].replace(/,/g, '') + ')'; x = x.join(''); alert(x);