How do I replace all occurrences of “/” in a strin

2020-01-29 01:47发布

For some reason the "".replace() method only replaces the first occurrence and not the others. Any ideas?

4条回答
疯言疯语
2楼-- · 2020-01-29 01:55

Try this code:

 text = text.replace(new RegExp("textToReplace","g"), "replacemntText")); 
查看更多
相关推荐>>
3楼-- · 2020-01-29 02:02
"Your/string".split("/").join("_")

if you don't require the power of RegExp

查看更多
我欲成王,谁敢阻挡
4楼-- · 2020-01-29 02:17

You have to use the g modifier (for global) in your replace call.

str = str.replace(/searchString/g, "replaceWith")

In your particular case it would be:

str = str.replace (/\//g, "_");

Note that you must escape the / in the regular expression.

查看更多
倾城 Initia
5楼-- · 2020-01-29 02:22
str.replace(/\//g,”_”)
查看更多
登录 后发表回答