【问题标题】:How do I remove a span tag in a string with Javascript?如何使用 Javascript 删除字符串中的 span 标签?
【发布时间】:2022-10-07 21:09:06
【问题描述】:

假设我有这个字符串(这是一个由 html 标签组成的字符串):

const str = "<li class='test'>
    <div class='myDiv' >
    <span class='myClass'>Person is a: </span>
    <a class='myLink' tabindex='0'> Great citizen. Really nice guy</a>
    </div>
    </li>"

我将如何删除 &lt;span&gt; 标签以及它们之间的所有内容,所以输出如下:

const str = "<li class='test'>
    <div class='myDiv' >
    <a class='myLink' tabindex='0'> Great citizen. Really nice guy</a>
    </div>
    </li>"

谢谢你的帮助!

【问题讨论】:

  • 好吧,我不会使用正则表达式(因为你已经标记了它)。它是 JS,所以使用 DOM 函数。

标签: javascript regex


【解决方案1】:

let str = `<li class='test'>
    <div class='myDiv' >
    <span class='myClass'>Person is a: </span>
    <a class='myLink' tabindex='0'> Great citizen. Really nice guy</a>
    </div>
    </li>`;

const reg = new RegExp("<span(.*?)span>", "gms");
console.log(str.replace(reg, ""));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-21
    • 1970-01-01
    • 2021-09-15
    • 2019-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多