Home > Software engineering >  Insert a quote in a string
Insert a quote in a string

Time:10-31

I'm looking to insert a quote in a string, but keep everything else. So, an example string: ' "2020-10-10",8000,"Hello" '

I want to put quotes around 8000 (or whatever number is there). so: ' "2020-10-10","8000","Hello" '

How would I do that in regex?

CodePudding user response:

I'm not an expert on regex but you can do it you just have to do it twice. Because I couldn't figure out a way to look for ",char or char,".

function test() {
  try {
    let a = ' "2020-10-10",8000,"Hello" ';
    a = a.replace(/,/g,'","');
    a = a.replace(/""/g,'"');
    console.log(a);
  }
  catch(err) {
    console.log(err);
  }
}

7:26:23 AM  Notice  Execution started
7:26:23 AM  Info     "2020-10-10","8000","Hello" 
7:26:23 AM  Notice  Execution completed
  • Related