JavaScript實現(xiàn)HTML轉(zhuǎn)換為純文本的幾種方法
1. 使用 DOMParser 和 textContent
DOMParser 可以將 HTML 字符串解析為 DOM 文檔,然后使用 textContent 屬性獲取純文本內(nèi)容。
function htmlToTextUsingDOMParser(html) {
const parser = new DOMParser();
const doc = parser.parseFromString(html, 'text/html');
return doc.body.textContent || '';
}
// 示例使用
const htmlString = '<p>這是一段 <strong>HTML</strong> 文本。</p>';
const plainText = htmlToTextUsingDOMParser(htmlString);
console.log(plainText);
2. 創(chuàng)建臨時 div 元素
創(chuàng)建一個臨時的 div 元素,將 HTML 內(nèi)容插入其中,然后使用 textContent 屬性獲取純文本。
function htmlToTextUsingDiv(html) {
const div = document.createElement('div');
div.innerHTML = html;
return div.textContent || '';
}
// 示例使用
const htmlString2 = '<h1>標(biāo)題</h1><p>段落內(nèi)容</p>';
const plainText2 = htmlToTextUsingDiv(htmlString2);
console.log(plainText2);
3. 正則表達(dá)式替換
使用正則表達(dá)式替換 HTML 標(biāo)簽,從而得到純文本。不過這種方法有局限性,因為正則表達(dá)式很難處理所有復(fù)雜的 HTML 標(biāo)簽情況。
function htmlToTextUsingRegex(html) {
return html.replace(/<[^>]*>?/gm, '');
}
// 示例使用
const htmlString3 = '<a href="#" rel="external nofollow" rel="external nofollow" >鏈接</a> 文本';
const plainText3 = htmlToTextUsingRegex(htmlString3);
console.log(plainText3);
以上三種方法各有優(yōu)缺點,DOMParser 和創(chuàng)建臨時 div 元素的方法更可靠,因為它們能正確處理 HTML 實體和嵌套標(biāo)簽;而正則表達(dá)式方法簡單但不夠健壯,僅適用于簡單的 HTML 字符串。
除了之前提到的方法,還有其他幾種將 HTML 轉(zhuǎn)換為純文本的方式,下面為你深入介紹:
4. 使用 Element.textContent(瀏覽器環(huán)境)
如果你在瀏覽器環(huán)境中,可以直接創(chuàng)建一個 div 元素并將 HTML 內(nèi)容插入其中,然后獲取 textContent 屬性。這種方法和前面創(chuàng)建臨時 div 元素類似,但更簡潔。
function htmlToTextUsingElement(html) {
const element = document.createElement('div');
element.innerHTML = html;
return element.textContent;
}
// 示例使用
const htmlString4 = '<span>這是一個 <em>示例</em>。</span>';
const plainText4 = htmlToTextUsingElement(htmlString4);
console.log(plainText4);
5. 使用 DOMPurify 庫
DOMPurify 是一個用于凈化 HTML 輸入的庫,它可以幫助你安全地移除 HTML 標(biāo)簽,同時避免 XSS 攻擊。在處理不可信的 HTML 輸入時,這是一個很好的選擇。
首先,你需要安裝 DOMPurify:
npm install dompurify
然后在代碼中使用:
import DOMPurify from 'dompurify';
function htmlToTextUsingDOMPurify(html) {
const clean = DOMPurify.sanitize(html, { ALLOWED_TAGS: [] });
return clean;
}
// 示例使用
const htmlString5 = '<a href="#" rel="external nofollow" rel="external nofollow" onclick="alert(\'XSS\')">危險鏈接</a>';
const plainText5 = htmlToTextUsingDOMPurify(htmlString5);
console.log(plainText5);
6. 使用 JSDOM(Node.js 環(huán)境)
如果你在 Node.js 環(huán)境中,可以使用 JSDOM 庫來模擬瀏覽器環(huán)境,然后獲取純文本內(nèi)容。
首先,安裝 JSDOM:
npm install jsdom
然后在代碼中使用:
const { JSDOM } = require('jsdom');
function htmlToTextUsingJSDOM(html) {
const dom = new JSDOM(html);
return dom.window.document.body.textContent;
}
// 示例使用
const htmlString6 = '<h2>Node.js 示例</h2><p>這是一個使用 JSDOM 的示例。</p>';
const plainText6 = htmlToTextUsingJSDOM(htmlString6);
console.log(plainText6);
方法對比
DOMParser和Element.textContent:適用于瀏覽器環(huán)境,簡單易用,能正確處理 HTML 實體和嵌套標(biāo)簽。DOMPurify:主要用于凈化 HTML 輸入,在處理不可信的 HTML 時非常有用,能有效防止 XSS 攻擊。JSDOM:適用于 Node.js 環(huán)境,模擬瀏覽器環(huán)境來處理 HTML,功能強大但引入了額外的依賴。- 正則表達(dá)式:簡單快速,但無法處理復(fù)雜的 HTML 結(jié)構(gòu)和嵌套標(biāo)簽,容易出錯。
根據(jù)你的具體需求和使用環(huán)境,選擇合適的方法來將 HTML 轉(zhuǎn)換為純文本。
以上就是JavaScript實現(xiàn)HTML轉(zhuǎn)換為純文本的幾種方法的詳細(xì)內(nèi)容,更多關(guān)于JavaScript HTML轉(zhuǎn)文本的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
js和jquery實現(xiàn)監(jiān)聽鍵盤事件示例代碼
這篇文章主要為大家介紹了js實現(xiàn)監(jiān)聽鍵盤事件示例代碼,監(jiān)聽鍵盤組合鍵CTRL+C,以便做出對應(yīng)的響應(yīng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-01-01
基于JS實現(xiàn)數(shù)字動態(tài)變化顯示效果附源碼
我們經(jīng)??吹揭壕щ娮颖順邮?,數(shù)字動態(tài)顯示,動態(tài)變化的在指定元素內(nèi)顯示數(shù)字。怎么實現(xiàn)效果呢?下面小編給大家?guī)砹嘶贘S實現(xiàn)數(shù)字動態(tài)變化顯示效果 ,感興趣的朋友一起看看吧2019-07-07

