如何使用replace()从HTML中删除垂直条“|”(How to remove a vertical bar “|” from HTML using replace())
我一直遇到这个问题一段时间了:
v = jQuery("span.count").html().replace(/\(|\)/g, ""); jQuery("span.count").html(v);我在SO上找到了这个对于括号非常有效但我似乎无法使用垂直条| 。
I've been having problems with this for a while now:
v = jQuery("span.count").html().replace(/\(|\)/g, ""); jQuery("span.count").html(v);I found this on SO which work great for brackets but I can't seem to get it to work with the vertical bar |.
最满意答案
| 是一个在正则表达式中用作“或”的字符。 你的正则表达式所说的是“用空字符串替换任何(无)或(无)的东西 ”。 逃避| 使用反斜杠:
string.replace(/\|/g, ""); // ^ Escape it!| is a character that is used in regex as "or". What your regex says is "replace anything that is (nothing) or (nothing) with an empty string". Escape the | using a backslash as such:
string.replace(/\|/g, ""); // ^ Escape it!更多推荐
发布评论