Javascript字符串替换不起作用 [英] Javascript string replace not working

查看:424
本文介绍了Javascript字符串替换不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个字符串(房间描述),并希望用一些新的字符串替换部分<?player> req.session.player )。

So I have a string (a room description) and would like to replace part <?player> of it with some new string (req.session.player).

以下是代码:

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

我已经过测试, req.session.player 确实有字符串值。

I have tested and the req.session.player does have string value.

当我执行替换方法时,没有任何变化。
注意:我也尝试使用 /<?player> / ,但这也不起作用。

When I do the replace method nothing changes. NOTE: I have also tried using /<?player>/ and this did not work either.

任何想法?

推荐答案

问题是未分配replace方法的返回值:

The problem is the returned value of the replace method is not assigned:

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

JS小提琴: http://jsfiddle.net/LEBRK/

这篇关于Javascript字符串替换不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆