admin管理员组文章数量:1323342
when I try the following it doesn't work: str.replace("| stuff", "")
But if I remove the PIPE it does? str.replace("stuff", "")
Why doesn't the JS function allow for the PIPE | ? What can I do to do a replace that includes a pipe?
when I try the following it doesn't work: str.replace("| stuff", "")
But if I remove the PIPE it does? str.replace("stuff", "")
Why doesn't the JS function allow for the PIPE | ? What can I do to do a replace that includes a pipe?
Share Improve this question edited Jan 20, 2010 at 5:59 peller 4,54320 silver badges21 bronze badges asked Jan 20, 2010 at 5:39 AnApprenticeAnApprentice 111k202 gold badges637 silver badges1k bronze badges 1- what is str, what is your result, and what are you trying to achieve? – peller Commented Jan 20, 2010 at 6:03
4 Answers
Reset to default 5Because .replace
accepts a RegExp, and |
is a special character in RegExp. You need to escape it.
For example, use str.replace(/\|/g, "")
to remove every |
character.
No, it should be working, unless you use /| stuff/
or RegExp("| stuff")
instead of "| stuff"
"xyz| stuff".replace("| stuff", ""); //returns xyz
Isn't it
"xyz| stuff".replace("\| stuff", ""); //returns xyz
str.replace("| stuff", "")
should work but will only replace the first occurrence. If you want to replace all of them, try a using a regex like str.replace(/\|\sstuff/g, "")
本文标签: JavaScript Replace String with a Character Stack Overflow
版权声明:本文标题:JavaScript Replace String with a Character | - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742134985a2422329.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论