簡體   English   中英

在沒有瀏覽器環境的情況下,在JS中將HTML轉換為純文本

[英]Convert HTML to plain text in JS without browser environment

我有一個 CouchDB 視圖映射函數,它生成一個存儲的 HTML 文檔的摘要(文本的前x字符)。 不幸的是,我沒有將 HTML 轉換為純文本的瀏覽器環境。

目前我使用這個多階段正則表達式

html.replace(/<style([\s\S]*?)<\/style>/gi, ' ')
    .replace(/<script([\s\S]*?)<\/script>/gi, ' ')
    .replace(/(<(?:.|\n)*?>)/gm, ' ')
    .replace(/\s+/gm, ' ');

雖然它是一個非常好的過濾器,但它顯然不是一個完美的過濾器,有時會漏掉一些殘留物。 有沒有更好的方法可以在沒有瀏覽器環境的情況下轉換為純文本?

這個簡單的正則表達式有效:

text.replace(/<[^>]*>/g, '');

它刪除所有錨點。

實體,例如&lt; 不包含 <,所以這個正則表達式沒有問題。

將 HTML 轉換為 Gmail 等純文本:

html = html.replace(/<style([\s\S]*?)<\/style>/gi, '');
html = html.replace(/<script([\s\S]*?)<\/script>/gi, '');
html = html.replace(/<\/div>/ig, '\n');
html = html.replace(/<\/li>/ig, '\n');
html = html.replace(/<li>/ig, '  *  ');
html = html.replace(/<\/ul>/ig, '\n');
html = html.replace(/<\/p>/ig, '\n');
html = html.replace(/<br\s*[\/]?>/gi, "\n");
html = html.replace(/<[^>]+>/ig, '');

如果您可以使用jQuery

var html = jQuery('<div>').html(html).text();

使用 TextVersionJS ( http://textversionjs.com ),您可以將 HTML 轉換為純文本。 它是純 javascript(帶有大量 RegExp),因此您可以在瀏覽器和 node.js 中使用它。

在 node.js 中,它看起來像:

var createTextVersion = require("textversionjs");
var yourHtml = "<h1>Your HTML</h1><ul><li>goes</li><li>here.</li></ul>";

var textVersion = createTextVersion(yourHtml);

(我從頁面復制了示例,您必須先 npm install 模塊。)

你可以試試這個方法。 textContentinnerText它們都不兼容所有瀏覽器:

var temp = document.createElement("div");
temp.innerHTML = html;
return temp.textContent || temp.innerText || "";

將 html 的 @EpokK 答案更新為電子郵件文本版本用例

const htmltoText = (html: string) => {
  let text = html;
  text = text.replace(/\n/gi, "");
  text = text.replace(/<style([\s\S]*?)<\/style>/gi, "");
  text = text.replace(/<script([\s\S]*?)<\/script>/gi, "");
  text = text.replace(/<a.*?href="(.*?)[\?\"].*?>(.*?)<\/a.*?>/gi, " $2 $1 ");
  text = text.replace(/<\/div>/gi, "\n\n");
  text = text.replace(/<\/li>/gi, "\n");
  text = text.replace(/<li.*?>/gi, "  *  ");
  text = text.replace(/<\/ul>/gi, "\n\n");
  text = text.replace(/<\/p>/gi, "\n\n");
  text = text.replace(/<br\s*[\/]?>/gi, "\n");
  text = text.replace(/<[^>]+>/gi, "");
  text = text.replace(/^\s*/gim, "");
  text = text.replace(/ ,/gi, ",");
  text = text.replace(/ +/gi, " ");
  text = text.replace(/\n+/gi, "\n\n");
  return text;
};

如果你想要一些准確的東西並且可以使用 npm 包,我會使用html-to-text

從自述文件:

const { htmlToText } = require('html-to-text');

const html = '<h1>Hello World</h1>';
const text = htmlToText(html, {
  wordwrap: 130
});
console.log(text); // Hello World

僅供參考,我在 npm 趨勢中發現了這一點; html-to-text似乎是我的用例的最佳選擇,但您可以在此處查看其他人。

很簡單,你也可以實現一個“toText”原型:

String.prototype.toText = function(){
    return $(html).text();
};

//Let's test it out!
var html = "<a href=\"http://www.google.com\">link</a>&nbsp;<br /><b>TEXT</b>";
var text = html.toText();
console.log("Text: " + text); //Result will be "link TEXT"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM