0

I have string that is dynamic and need to change along with escape character. Suppose

var key = "Any String";
var term = "^"+key+"^";
var description = description.replace(term,"replacement_string");

it replace only first occurrence and works well.it changes like this:

Lorem ipsum dolor sit amet ^Any String^ and Any String Lorem ipsum dolor sit amet ^Any String^

to

Lorem ipsum dolor sit amet replacement_string and Any String Lorem ipsum dolor sit amet ^Any String^

if I use replace globally then it doesn't work

var key = "Any String";
var term = "/\^"+key+"\^/g";
var description = description.replace(term,"replacement_string");

print like this:

Lorem ipsum dolor sit amet ^Any String^ and Any String Lorem ipsum dolor sit amet ^Any String^

I need to change all occurance of ^Any String^ not if Any String doesn't have ^ then it shouldn't change the word. Result should be like this.

This is a description with replacement_string and Any String also need to change replacement_string

Rajkumar Sharma
  • 479
  • 1
  • 3
  • 9

0 Answers0