如何替换所有出现的“/”在带有“_”的字符串中在JavaScript? [英] How do I replace all occurrences of "/" in a string with "_" in JavaScript?
本文介绍了如何替换所有出现的“/”在带有“_”的字符串中在JavaScript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
由于某种原因,。replace()
方法只替换第一个匹配项而不替换其他匹配项。有什么想法?
For some reason the "".replace()
method only replaces the first occurrence and not the others. Any ideas?
推荐答案
你必须使用 g
修饰符(对于你的替换电话中的全局)。
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.
这篇关于如何替换所有出现的“/”在带有“_”的字符串中在JavaScript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文