29

x保存されたHTMLドキュメントの要約(テキストの最初の文字)を生成するCouchDBビューマップ関数があります。残念ながら、HTMLをプレーンテキストに変換するためのブラウザ環境がありません。

現在、この多段階正規表現を使用しています

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

非常に優れたフィルターですが、明らかに完璧なフィルターではなく、残り物がすり抜けることもあります。ブラウザ環境なしでプレーンテキストに変換するためのより良い方法はありますか?

4

7 に答える 7

27

この単純な正規表現は機能します。

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

すべてのアンカーを削除します。

エンティティに&lt;は<が含まれていないため、この正規表現に問題はありません。

于 2013-03-02T22:31:41.463 に答える
13

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();
于 2013-11-19T12:36:48.650 に答える
10

TextVersionJS(http://textversionjs.com)を使用すると、HTMLをプレーンテキストに変換できます。これは純粋なJavaScript(大量の正規表現を含む)であるため、ブラウザーや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でインストールする必要があります。)

于 2016-07-27T12:14:54.963 に答える
6

この方法で試すことができます。どちらもすべてのブラウザと互換性がありませんtextContentinnerText

var temp = document.createElement("div");
temp.innerHTML = html;
return temp.textContent || temp.innerText || "";
于 2018-04-13T02:11:12.010 に答える
3

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;
};

于 2020-12-04T22:02:36.600 に答える
0

正確なものが必要で、npmパッケージを使用できる場合は、html-to-textを使用します。

READMEから:

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は私のユースケースに最適なオプションのように見えましたが、ここで他のオプションを確認できます。

于 2021-02-28T00:30:57.937 に答える
-4

非常にシンプルで、「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"
于 2016-02-27T19:31:51.620 に答える