如何在JavaScript字符串中全局替换正斜杠? [英] How to globally replace a forward slash in a JavaScript string?

查看:109
本文介绍了如何在JavaScript字符串中全局替换正斜杠?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在JavaScript字符串中全局替换正斜杠?

How to globally replace a forward slash in a JavaScript string?

推荐答案

以下情况可以,但只会替换一次出现:

The following would do but only will replace one occurence:

"string".replace('/', 'ForwardSlash');

对于全局替换,或者如果您更喜欢正则表达式,您只需要避开斜杠: / p>

For a global replacement, or if you prefer regular expressions, you just have to escape the slash:

"string".replace(/\//g, 'ForwardSlash');

这篇关于如何在JavaScript字符串中全局替换正斜杠?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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