簡體   English   中英

刪除 html 標簽和 html 實體?

[英]Removing both html tags and html entities?

嗨,我得到一個來自 api 的字符串,就像這樣。

<p>Be prepared before getting on top of the maountain. Check your speed before rumbling over the wet armored section that make a tight right hand turn into a bridge. This trail is known for its flow. Butcher Ranch is the main trail for the classic Downieville Downhill. Use some caution in the corners here. Watch that you don&#39;t overshoot the switchback to stay on the black diamond descent - the blue route is completely overgrown.</p>\n

如您所見,它同時具有 html 標簽和 html 實體。

我可以用這一行刪除 html 標簽,但是.replace(/<\/?[^>]+(>|$)/g, '')

我不知道如何在 1 行中同時刪除 html 實體和標簽

如果您可以將這些 HTML 實體替換為相應的字符,那么DOMParser就是 go 的方法:

 const str = `<p>Be prepared before getting on top of the maountain. Check your speed before rumbling over the wet armored section that make a tight right hand turn into a bridge. This trail is known for its flow. Butcher Ranch is the main trail for the classic Downieville Downhill. Use some caution in the corners here. Watch that you don&#39;t overshoot the switchback to stay on the black diamond descent - the blue route is completely overgrown.</p>\n`; const parsed = new DOMParser().parseFromString( str, "text/html" ).documentElement.textContent; console.log( parsed );

暫無
暫無

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

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