使用正则表达式删除所有 html 属性(替换)

bra*_*r19 1 html javascript regex

例如我有这样的html:

\n\n
<title>Ololo - text\xe2\x80\x99s life</title><div class="page-wrap"><div class="ng-scope"><div class="modal custom article ng-scope in" id="new-article" aria-hidden="false" style="display: block;"><div class="modal-dialog first-modal-wrapper">< div class="modal-content"><div class="modal-body full long"><div class="form-group">olololo<ul style="color: rgb(85, 85, 85);background-color: rgb(255, 255, 255);"><li>texttext</li><li>Filter the events lists by host.</li><li>Create graphs for separate hosts and for the groups of hosts.</li></ul><p style="color: rgb(85, 85, 85);background-color: rgb(255, 255, 255);">bbcvbcvbcvbcvbcvbcvbcvb</p></div></div></div></div></div></div><title>cvbcbcvbcvbcvbccb</title><div class="page-wrap"></div></div>\n
Run Code Online (Sandbox Code Playgroud)\n\n

我怎样才能从这样的html中删除所有样式类id等?

\n\n

我有这样的正则表达式:

\n\n
/<([a-z][a-z0-9]*)[^>]*?(\\/?)>/i\n
Run Code Online (Sandbox Code Playgroud)\n\n

怎么了?如何借助正则表达式删除所有html属性?

\n\n

这是小提琴:

\n\n

http://jsfiddle.net/qL4maxn0/1/

\n

Wil*_*sem 6

首先,我建议你在这种情况下不要使用正则表达式,它们并不意味着解析 HTML 等树形结构。

\n\n

但是,如果您没有选择,我认为对于所请求的问题,您可以使用正则表达式。

\n\n

在我看来,您好像忘记了空格、重音符号等。您可以利用不允许使用大于>和小于符号作为原始文本的事实。<

\n\n
/<\\s*([a-z][a-z0-9]*)\\s.*?>/gi\n
Run Code Online (Sandbox Code Playgroud)\n\n

并用以下方式调用它:

\n\n
result = body.replace(regex, '<$1>')\n
Run Code Online (Sandbox Code Playgroud)\n\n

对于您给定的样本,它会产生:

\n\n
<title>Ololo - text\xe2\x80\x99s life</title><div><div><div><div><div><div><div>olololo<ul><li>texttext</li><li>Filter the events lists by host.</li><li>Create graphs for separate hosts and for the groups of hosts.</li></ul><p>bbcvbcvbcvbcvbcvbcvbcvb</p></div></div></div></div></div></div><title>cvbcbcvbcvbcvbccb</title><div></div></div>\n
Run Code Online (Sandbox Code Playgroud)\n