javascript - 移除
问题描述
我正在寻找一个 javascript 正则表达式,通过它我可以<p><br><p>
从我的内容中删除标签。
例如:
下面一个是我的内容
<p><br></p>
<p>function removes whitespace or other predefined characters from the right side of a string.</p>
<p><br></p>
<p><br/><p>
我正在寻找这个
<p>function removes whitespace or other predefined characters from the right side of a string.</p>
我正在使用此代码,但它不起作用
function rtrim(str) {
if(!str) return str;
return str.replace(/\s+$/g, '');
}
console.log(rtrim(string));
解决方案
您想删除 HTML 换行符<br/>
及其周围的段落元素<p>
,而不是空格,您可以使用当前的正则表达式。
\s+匹配任何空白字符(等于 [\r\n\t\f\v ])
在您的情况下,这应该是正确的正则表达式<p><br[\/]?><[\/]?p>
function rtrim(str) {
if(!str) return str;
return str.replace(/<p><br[\/]?><[\/]?p>/g, '');
}
console.log(rtrim("<p><br></p><p>function removes whitespace or other predefined characters from the right side of a string.</p><p><br></p><p><br/><p>"));
我曾经<br[\/]?>
确保带和不带正斜杠的换行符都匹配。
推荐阅读
- javascript - 在javascript中将nodeList返回给调用函数的正确方法是什么?
- scala - 优化包含循环或递归的简单函数
- c# - 进程完成后关闭 SQL 连接?
- angularjs - 如何将具有预定义参数的方法传递给 Angular JS 指令
- python - numpy 数据类型的问题?
- rmysql - 我的本地主机中有学生数据库,但它仍然显示这个
- kubernetes - 如何为 Kubernetes 集群中运行的所有不同应用程序设置单独的 DNS 端点???(AWS/GCP/天蓝色)
- android - 如何在 MoPub 中集成 Chartboost 中介?
- nginx - GRPC 流在写入之前关闭(通过 nginx 的 grpc_pass)
- reactjs - 如何描述何时在反应中使用术语“方法”与“功能”?