String replacement but only for the substring within quotes

You could get the string within quotes using "([^"]+)" (Regex demo). This will get the string without the quotes to a capturing group. Then, replace all the , with - in the matched string.

The second parameter to replace is function, The first parameter of this replacer function is the entire matched substring, including the quotes. The second parameter is the capturing group. Since you want to to remove the " from the output, you can directly use the capturing group parameter.

const input = `hello,"sai,sur",ya,teja`,
      output = input.replace(/"([^"]+)"/g, (_, g) => g.replace(',', '-'))


One approach would be to match all quoted terms and then use a callback function to replace commas with dash:

var input = "hello,\"sai,sur\",ya,teja";
var output = input.replace(/"(.*?)"/g, (match, startIndex, wholeString) => {
    return match.replace(",", "-");
console.log("input:  " + input);
console.log("output: " + output);

const result = 'hello,"sai,sur",ya,teja'.replace(/(.*)"(.*)(,)(.*)"(.*)/,  '$1$2-$4$5')


The keyword of this technique is "Regex group replace"