在保留换行符(使用JavaScript)的同时,将HTML转换为纯文本最方便的方法是什么?
基本上,我只需要从浏览器窗口复制HTML并将其粘贴到textarea元素中 例如,我想要这个:在保留换行符(使用JavaScript)的同时,将HTML转换为纯文本最方便的方法是什么?,javascript,html,plaintext,Javascript,Html,Plaintext,基本上,我只需要从浏览器窗口复制HTML并将其粘贴到textarea元素中 例如,我想要这个: <p>Some</p> <div>text<br />Some</div> <div>text</div> 三个步骤 First get the html as a string. Second, replace all <BR /> and <BR> with \r\n. Third, us
<p>Some</p>
<div>text<br />Some</div>
<div>text</div>
三个步骤
First get the html as a string.
Second, replace all <BR /> and <BR> with \r\n.
Third, use the regular expression "<(.|\n)*?>" to replace all markup with "".
首先将html作为字符串获取。
第二,将所有
和
替换为\r\n。
第三,使用正则表达式“”将所有标记替换为“”。
我试图找到我不久前为此编写的一些代码。它工作得很好。让我概述一下它的作用,希望你能复制它的行为
- 用alt或标题文本替换图像
- 将链接替换为“文本[链接]”
- 替换通常会产生垂直空白的内容。h1-h6、div、p、br、hr等(我知道,我知道。这些实际上可能是内联元素,但效果很好)
- 去掉其余的标记,并替换为空字符串
public static string Convert(string template)
{
template = Regex.Replace(template, "<img .*?alt=[\"']?([^\"']*)[\"']?.*?/?>", "$1"); /* Use image alt text. */
template = Regex.Replace(template, "<a .*?href=[\"']?([^\"']*)[\"']?.*?>(.*)</a>", "$2 [$1]"); /* Convert links to something useful */
template = Regex.Replace(template, "<(/p|/div|/h\\d|br)\\w?/?>", "\n"); /* Let's try to keep vertical whitespace intact. */
template = Regex.Replace(template, "<[A-Za-z/][^<>]*>", ""); /* Remove the rest of the tags. */
return template;
}
公共静态字符串转换(字符串模板)
{
template=Regex.Replace(模板“,“$1”);/*使用图像替换文本*/
template=Regex.Replace(模板“,”$2[$1]”;/*将链接转换为有用的内容*/
template=Regex.Replace(template,“,“\n”);/*让我们尝试保持垂直空白不变*/
template=Regex.Replace(模板“,”);/*删除其余标记*/
返回模板;
}
如果该HTML在您的网页中可见,您可以通过用户选择(或者在IE中只使用TextRange
)来实现。这会保留换行符,如果不一定是前导空格和尾随空格的话
2012年12月10日更新
但是,选择对象的toString()
方法在浏览器之间是不一致的,并且工作不一致,因此这种方法的基础不稳固,我不建议现在使用它。如果这个答案不被接受,我会删除它
演示:
代码:
我根据这个答案做了一个函数:
函数html文本(html){
//拆下代码制动器和卡舌
html=html。替换(/\n/g,“”);
html=html。替换(/\t/g,“”);
//保持html制表符和制表符
html=html.replace(//g,“\t”);
html=html.replace(//g,“\n”);
html=html.replace(//g,“\n”);
html=html.replace(//g,“\n”);
html=html.replace(//g,“\n”);
html=html.replace(//g,“\n”);
html=html.replace(//
/g,“\n”);html=html.replace(//g,“\n”);
//将html解析为文本
var dom=(new DOMParser()).parseFromString(“”+html,'text/html');
返回dom.body.textContent;
}
根据答案,我必须将基本HTML电子邮件模板转换为纯文本版本,作为node.js中构建脚本的一部分。我必须使用它才能工作,但以下是我的代码:
const htmlToText = (html) => {
html = html.replace(/\n/g, "");
html = html.replace(/\t/g, "");
html = html.replace(/<\/p>/g, "\n\n");
html = html.replace(/<\/h1>/g, "\n\n");
html = html.replace(/<br>/g, "\n");
html = html.replace(/<br( )*\/>/g, "\n");
const dom = new JSDOM(html);
let text = dom.window.document.body.textContent;
text = text.replace(/ /g, "");
text = text.replace(/\n /g, "\n");
text = text.trim();
return text;
}
consthtmltotext=(html)=>{
html=html。替换(/\n/g,“”);
html=html。替换(/\t/g,“”);
html=html.replace(//g,“\n\n”);
html=html.replace(//g,“\n\n”);
html=html.replace(/
/g,“\n”);
html=html.replace(//g,“\n”);
const dom=新的JSDOM(html);
让text=dom.window.document.body.textContent;
text=text.replace(//g,“”);
text=text.replace(/\n/g,“\n”);
text=text.trim();
返回文本;
}
不幸的是,这种方法忽略了两个段落或DIV之间出现的换行符。在执行regex replace之前,在每个close P和DIV标记后插入硬换行符,这不是很容易解决的问题吗?嗯,问题有点深。我需要得到与用户在屏幕上看到的内容相似的文本。例如,如果有两个段落('p'元素),并且它们都有标准边距,我希望在相应的文本片段之间获得两个换行符。但是,当边距为0时,它需要是一个换行符。剪贴板就是这样工作的——至少在某些浏览器中是这样。。。那不是Javascript,不是吗?也没有直接回答这个问题,因为这个问题真的涉及到复制和粘贴。语言真的不重要,它是如何进行的。这可以很容易地移植到JS。我只是在展示我过去做过的事情。谢谢。很像。然而,不幸的是,结果并不完全是用户所看到的。例如,Convert(“Sometext”)和Convert(“Sometext”)会给出不同的结果,而浏览器会以相同的方式呈现这些结果。问题是文本的显示顺序。事物的布局方式并不总是与标记层次结构相关。可能与Thank的重复。有趣的是,在非IE的情况下(第一个块),它会得到要复制到剪贴板的内容,但在IE的情况下(第二个块),它不是相同的字符串。IE和非IE字符串之间的区别是什么?第一个块使用Selection的
toString()
方法仅提取所选内容的文本(而不是复制到剪贴板的富文本),因此它们应该或多或少相同。对不起,我的意思是,复制剪贴板中页面片段得到的字符串与函数返回的字符串不同。IE就是这样,对于非IE浏览器,这两个字符串是相同的。该函数本身非常适合我在问题中描述的问题(IE的东西除外,这并不重要)。@hienbt88:它当然是建立在不可靠的基础上的:Selection.toString()不是标准化的,在浏览器之间的工作方式不同,并且在IE 9中不保留换行符(从这个答案的原始版本开始发布)。但是,它仍然保留了Mozilla、WebKit和Opera当前版本中的换行符,而且因为我刚刚对它进行了调整,也就是说,老实说,我不会长期推荐这种方法。@SaifUllah:是的。这是一个
function getInnerText(el) {
var sel, range, innerText = "";
if (typeof document.selection != "undefined" && typeof document.body.createTextRange != "undefined") {
range = document.body.createTextRange();
range.moveToElementText(el);
innerText = range.text;
} else if (typeof window.getSelection != "undefined" && typeof document.createRange != "undefined") {
sel = window.getSelection();
sel.selectAllChildren(el);
innerText = "" + sel;
sel.removeAllRanges();
}
return innerText;
}
function htmlToText(html){
//remove code brakes and tabs
html = html.replace(/\n/g, "");
html = html.replace(/\t/g, "");
//keep html brakes and tabs
html = html.replace(/<\/td>/g, "\t");
html = html.replace(/<\/table>/g, "\n");
html = html.replace(/<\/tr>/g, "\n");
html = html.replace(/<\/p>/g, "\n");
html = html.replace(/<\/div>/g, "\n");
html = html.replace(/<\/h>/g, "\n");
html = html.replace(/<br>/g, "\n"); html = html.replace(/<br( )*\/>/g, "\n");
//parse html into text
var dom = (new DOMParser()).parseFromString('<!doctype html><body>' + html, 'text/html');
return dom.body.textContent;
}
const htmlToText = (html) => {
html = html.replace(/\n/g, "");
html = html.replace(/\t/g, "");
html = html.replace(/<\/p>/g, "\n\n");
html = html.replace(/<\/h1>/g, "\n\n");
html = html.replace(/<br>/g, "\n");
html = html.replace(/<br( )*\/>/g, "\n");
const dom = new JSDOM(html);
let text = dom.window.document.body.textContent;
text = text.replace(/ /g, "");
text = text.replace(/\n /g, "\n");
text = text.trim();
return text;
}