使用 JavaScript 剥离标签并处理换行符
我想从 html 中删除标签,但保留它的换行符。
我想要像在浏览器中复制文本并将其粘贴到记事本中这样的行为。
例如,将
转换为x1x2x1\nx2
到的代码;x1
x2
x1\nx2
x1x2
至x1x2
x1
至
x2x1\nx2
删除所有标签不起作用 (/<.*?>/g)。 还创建一个虚拟
并设置它的
innertHTML
并读取它的 textContent
将删除换行符。有帮助吗?
I want to strip tags from a html, but preserves it's line breaks.
I want the behaviour like copying the text in browser and pasting it in notepad.
For example, a code that converts:
<div>x1</div><div>x2</div>
tox1\nx2
<p>x1</p><p>x2</p>
tox1\nx2
<b>x1</b><i>x2</i>
tox1x2
x1<br>x2
tox1\nx2
Removing all tags not works (/<.*?>/g).
Also creating a dummy <div> and settings it's innertHTML
and read it's textContent
will remove line breaks.
Any Help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这对你来说怎么样?这会将出现的所有
、和
替换为
\ n
,然后剥离剩余的标签。虽然很愚蠢,但至少是一个开始。然而,这并不适用于所有 HTML。只是你提到的标签。
How's this work for you? This will replace every occurrence of
<br>
,</div>
, and</p>
with a\n
, and then strip the remaining tags. Its goofy, but its at least a start.This doesn't work for all HTML, however. Just the tags you mentioned.
尝试:
这将剥离标签并用新行替换
或
,但是为块元素添加新行需要相当长的时间想出一个解决办法。这是一个演示
Try:
This will strip the tags and replace
<br />
or<br>
with new lines, but adding new lines for block elements requires quite some time to come up with a solution.Here is a demo
这是我在感到无聊之前所得到的……
This is as far as I got before I got bored...
现在,您可以使用此函数,
该函数会将所有开始和结束标记替换为空,并将
标记替换为换行符。这应该会给你想要的输出。You can use this
Now the function will replace all opening and closing tags with nothing, and
<br>
tags with line breaks. This should give you the desired output.