首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用html内容替换div中的特定内容

用html内容替换div中的特定内容
EN

Stack Overflow用户
提问于 2022-10-31 10:40:58
回答 1查看 50关注 0票数 -1

我想用内部包装的html元素替换div中的部分内容。

实际内容:

代码语言:javascript
复制
line 120, col 8: The relative URL 'Helloo' for attribute 'href' in tag 'a' is disallowed. (see undefined)line 136, col 16: The relative URL 'fasdf' for attribute 'href' in tag 'a' is disallowed. (see undefined)line 155, col 12: Missing URL for attribute 'action-xhr' in tag 'form'. (see https://amp.dev/documentation/components/amp-form)

预期输出:

代码语言:javascript
复制
    <ul>
      <li>The relative URL 'Helloo' for attribute 'href' in tag 'a' is disallowed. (see undefined)</li>
      <li>The relative URL 'fasdf' for attribute 'href' in tag 'a' is disallowed. (see undefined)</li>
      <li>Missing URL for attribute 'action-xhr' in tag 'form'. (see https://amp.dev/documentation/components/amp-form)</li>
    </ul>

generated)

  • Create

  • 删除行没有任何信息(行号将是每个错误消息

的动态

  • 列表项)。
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-10-31 12:45:03

这并不是很复杂。Regex可以帮助您。我使用regex查找每一行中的第一行,并使用.split()拆分行。

代码语言:javascript
复制
str = str.split(/line \d+, col \d+: /g).filter(x => x != "").map(x => `<li>${x}</li>`).join('')
str = `<ul>${str}</ul>`

代码语言:javascript
复制
let str = `line 120, col 8: The relative URL 'Helloo' for attribute 'href' in tag 'a' is disallowed. (see undefined)line 136, col 16: The relative URL 'fasdf' for attribute 'href' in tag 'a' is disallowed. (see undefined)line 155, col 12: Missing URL for attribute 'action-xhr' in tag 'form'. (see https://amp.dev/documentation/components/amp-form)`;

str = str.split(/line \d+, col \d+: /g).filter(x => x != "").map(x => `<li>${x}</li>`).join('')
str = `<ul>${str}</ul>`

console.log(str)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74262047

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档