【发布时间】:2021-01-11 07:20:37
【问题描述】:
我尝试将 html 文本转换为内联文本,标签之间或标签内的文本之间没有空格,但文本不会丢失空格。查看下面的代码以获取更多信息。
This is my html from which I want to remove the whitespaces
const html = `
<div>
<div>
<span>This is a text</span>
<div>
Another text
</div>
</div>
</div>
`;
我尝试“编写”下面的正则表达式,但它是错误的。我现在也删除标签。我尝试了不同的正则表达式方法,但我无法创建(或理解)正则表达式代码。
我愿意:
console.log(html.replace(/\>(\s+?)\</g,''))
输出:
<divdivspan>This is a text</spandiv>
Another text
</div/div/div>
我想:
console.log(html.replace('(regex)', ''))
output: <div><div><span>This is a text</span><div>Another text</div></div></div>
【问题讨论】:
标签: javascript regex