如何在文本块中查找和替换字符串?

AnA*_*ice 2 javascript replace

给出一串字符串:

var copy = "Hello world what is the day @James Bond blah bah blah blah...";
Run Code Online (Sandbox Code Playgroud)

使用jQuery/JS,给出:

var term = "James Bond";
var id = "XXXXXX";
Run Code Online (Sandbox Code Playgroud)

我怎样才能找到所有匹配'@' + term并返回如下内容:

"Hello world what is the day @[XXXXXX:James Bond] blah bah blah blah..."
Run Code Online (Sandbox Code Playgroud)

谢谢

gus*_*tkg 5

只需使用替换功能:

var term = "James Bond", id = "XXXXXXX";
"Hello world what is the day @James Bond blah bah blah blah...".replace("@"+term, "@["+id+":"+term+"]");
Run Code Online (Sandbox Code Playgroud)