13

基本的には、ブラウザウィンドウからそのHTMLをコピーして、textarea要素に貼り付けるだけの効果が必要です。

たとえば、私はこれが欲しいです:

<p>Some</p>
<div>text<br />Some</div>
<div>text</div>

これになるために:

Some
text
Some
text
4

5 に答える 5

19

そのHTMLがWebページ内に表示されている場合は、ユーザーを選択して(またはTextRange、IEでのみ)表示できます。これにより、必ずしも先頭と末尾の空白でなくても、改行が保持されます。

2012年12月10日更新

ただし、オブジェクトのtoString()メソッドはまだ標準化されておらず、ブラウザ間で一貫性のない動作をするため、このアプローチは不安定な状況に基づいており、現在は使用しないことをお勧めします。受け入れられなかった場合は、この回答を削除します。Selection

デモ: http: //jsfiddle.net/wv49v/

コード:

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;
}
于 2010-09-28T13:57:49.670 に答える
7

しばらく前に、このために書いたコードを見つけようとしました。それはうまくいきました。それが何をしたかを概説させてください、そしてうまくいけば、あなたはその振る舞いを複製することができます。

  • 画像を代替テキストまたはタイトルテキストに置き換えます。
  • リンクを「text[link]」に置き換えます
  • 一般的に垂直方向の空白を生成するものを置き換えます。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;
}
于 2010-09-28T13:44:00.517 に答える
5

私はこの答えに基づいて関数を作成しました:https ://stackoverflow.com/a/42254787/3626940

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;
}
于 2018-06-12T17:16:09.910 に答える
1

chrmcpnの回答に基づいて、node.jsのビルドスクリプトの一部として、基本的なHTMLメールテンプレートをプレーンテキストバージョンに変換する必要がありました。JSDOMを使用して機能させる必要がありましたが、コードは次のとおりです。

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;
}
于 2019-03-05T13:16:45.393 に答える
-2

3つのステップ。

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 "".
于 2010-09-28T13:37:45.917 に答える