0

I have a string e.g. My column name is Start Date (GMT)

I want to replace Start Date (GMT) portion with start_date.

I am trying to do it like:

    let a= "My column name is Start Date (GMT)";
    let b = "Start Date (GMT)";
    let str = new RegExp(b,"g");
    console.log(a.replace(str,"start_date"));

With same code I am able to replace the string with desired value if brackets are not there, but since bracket '(' is there it is not able to identify the string and replace it.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563

1 Answers1

0

This would work.

while loop will run till all the Start Date (GMT) are replaced by start_date.

let str = "My column name is Start Date (GMT) Start Date (GMT)";

while(str != str.replace('Start Date (GMT)', 'start_date')) {
  str = str.replace('Start Date (GMT)', 'start_date');
}

console.log(str);
vrintle
  • 5,501
  • 2
  • 16
  • 46