Javascript字符串替换不工作

Javascript字符串替换不工作,javascript,Javascript,因此,我有一个字符串(房间描述),希望替换零件。问题是没有指定替换方法的返回值: description = description.replace("<?player>", req.session.player); description=description.replace(问题是没有分配replace方法的返回值: description = description.replace("<?player>", req.session.player); descr

因此,我有一个字符串(房间描述),希望替换零件
。问题是没有指定替换方法的返回值:

description = description.replace("<?player>", req.session.player);

description=description.replace(问题是没有分配replace方法的返回值:

description = description.replace("<?player>", req.session.player);

description=description.replace(您必须将变量分配给新修改的字符串,因为
replace
不会更新变量:

var description = "<?player>, you are in a room.";
description = description.replace('<?player>', req.session.player);
var description = "<?player>, you are in a room.";

description = description.replace("<?player>", 'Bill'); // description now is "Bill, you are in a room."

有关完整信息,请阅读。一些引用:

返回一个新字符串,其中部分或全部匹配的
模式
已替换为
替换

此方法不会更改调用它的字符串对象。它只返回一个新字符串

要执行全局搜索和替换,请在正则表达式中包含g开关


您必须将变量分配给新修改的字符串,因为
replace
不会更新变量:

var description = "<?player>, you are in a room.";
description = description.replace('<?player>', req.session.player);
var description = "<?player>, you are in a room.";

description = description.replace("<?player>", 'Bill'); // description now is "Bill, you are in a room."

有关完整信息,请阅读。一些引用:

返回一个新字符串,其中部分或全部匹配的
模式
已替换为
替换

此方法不会更改调用它的字符串对象。它只返回一个新字符串

要执行全局搜索和替换,请在正则表达式中包含g开关


replace
方法,因此需要将其分配给
说明
变量:

var description = "<?player>, you are in a room.";
description = description.replace('<?player>', req.session.player);
var description = "<?player>, you are in a room.";

description = description.replace("<?player>", 'Bill'); // description now is "Bill, you are in a room."

var description=“
replace
方法,因此需要将其分配给
description
变量:

var description = "<?player>, you are in a room.";
description = description.replace('<?player>', req.session.player);
var description = "<?player>, you are in a room.";

description = description.replace("<?player>", 'Bill'); // description now is "Bill, you are in a room."

var描述=”有人想解释这次否决票吗?我想知道我如何才能改进这个答案。就是这样!非常感谢!!不需要改进。这是一个非常好的答案。@vickimagoo很高兴我能提供帮助!有人想解释这次否决票吗?我想知道我如何才能改进这个答案。就是这样!谢谢非常好!!不需要改进。这是一个非常好的答案。@vickimagoo很高兴我能帮上忙!今天有人对好的建议真的有问题。没有理由对此投反对票。@KevinBowersox有人可以投反对票的唯一原因是因为我的答案没有给以前的帖子添加任何新内容。我道歉,沉默的反对者-在回答问题之前,我没有刷新页面,也没有看到有人已经回答了。我认为问题在于代码最初没有实现建议。@woolstar我所在的城市现在是凌晨两点,我几乎要睡觉了。谢谢你在我的回答中指出问题!有人真的有问题今天我有一个很好的建议。没有理由投反对票。@KevinBowersox有人可以投反对票的唯一原因是因为我的回答没有给以前的帖子添加任何新内容。我很抱歉,沉默的反对票-我在回答问题之前没有刷新页面,也没有看到人们已经回答了。我认为问题在于代码最初没有实现建议。@woolstar我所在的城市现在是凌晨两点,我几乎要睡觉了。谢谢你在我的回答中指出了这个问题!