如何使用 JavaScript 字符串替换方法来替换 '+'符号?
您需要知道的一切都在问题中。我的 URL 中有一个搜索查询,如下所示:
http://www.mysite。 com?param=word1+word2+word3
到目前为止,我可以检索“word1+word2+word3”,但我似乎无法使用以下内容:
document.write(str.replace(/+/g," "));
我希望最终输出如下所示:“word1 word2 word3”
感谢您的浏览。
All you need to know is in the question. I have a search query in the URL that looks like this:
http://www.mysite.com?param=word1+word2+word3
So far, I can retrieve "word1+word2+word3" but I can't seem to use something like:
document.write(str.replace(/+/g," "));
I want the final output to look like this: "word1 word2 word3"
Thanks for looking.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尝试使用转义字符
\
转义+
符号,因为它是受限制的字符:http://www.regular-expressions.info/javascript.html
Try to escape the
+
sign using the escape char\
since is a restricted char:http://www.regular-expressions.info/javascript.html
+
是正则表达式中的一个特殊字符,如果你想按字面匹配它,你必须转义它。所以尝试:The
+
is a special character in regex, if you want to match it literally you have to escape it. So try:您必须转义
+
符号,它在正则表达式中具有特殊含义。You have to escape the
+
sign, it's has special meaning in Regexes.怎么样使用
split()
函数,这似乎对于这种情况要简单得多:How about using the
split()
function, that seems to be much simpler for this case:尝试使用十六进制值
Try with the hex value