jquery 将div中的特定内容替换为html内容

ehxuflar  于 2022-11-03  发布在  jQuery
关注(0)|答案(1)|浏览(199)

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

实际内容:

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)

预期输出:

<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>
  • 删除行号信息(行号将动态生成)
  • 为每个错误消息创建列表项
mrwjdhj3

mrwjdhj31#

它不是很复杂。正则表达式可以帮助你。我用正则表达式来查找每一行的第一行,用**.split()**来拆分行

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

相关问题