如何使用replace()从HTML中删除垂直条“

编程入门 行业动态 更新时间:2024-10-24 06:29:31
如何使用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!

更多推荐

本文发布于:2023-07-26 19:24:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1279902.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   replace   HTML

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!