首页 > 解决方案 > 更改 JavaScript 函数以删除 HTML 标记,除了

问题描述

我有以下函数可以消除 html 代码的所有标记:

function saringtags(r, l) {
    for (var e = r.split("<"), n = 0; n < e.length; n++) - 1 != e[n].indexOf(">") && (e[n] = e[n].substring(e[n].indexOf(">") + 1, e[n].length));
    return e = e.join(""), e = e.substring(0, l - 1)
}

问题在于:

<div class="lyric">
<blockquote>
It's nine o'clock on a Saturday<br>
The regular crowd shuffles in<br>
There's an old man sitting next to me<br>
Makin' love to his tonic and gin<br>
</blockquote>
</div>

变成这样:

It's nine o'clock on a SaturdayThe regular crowd shuffles inThere's an old man sitting next to meMakin' love to his tonic and gin

<br>如何为函数中的标记创建异常?这个结果是我需要的:

It's nine o'clock on a Saturday<br>The regular crowd shuffles in<br>There's an old man sitting next to me<br>Makin' love to his tonic and gin

标签: javascripthtml

解决方案


使用正则表达式:

/<.*[^br]>/g

html.replace(/<.*[^br]>/g,'')

https://regexr.com/58l77


推荐阅读