簡體   English   中英

等價於轉換字符實體的Javascript

[英]Javascript equivalent for converting character entities

我有以下HTML代碼段,其中JavaScript嵌入為一行。

<div class="social_icon twitter" onclick="shareSocialWall('twitter','New Comment on xxxx - WTF, if the dancers don&amp;acirc;&amp;#128;&amp;#153;t come in until 4ish','https:/xxxx')"></div>

如您所見,有一個onclick事件觸發了shareSocialWall 這里重要的是文字dancers don&amp;acirc;&amp;#128;&amp;#153;t

當文本傳遞給函數shareSocialWall ,將發生以下情況:

 location='https://twitter.com/intent/tweet?text='+text+'&url='+encodeURIComponent(url);

問題是文本中斷了執行此tweet的電話。 有沒有一種方法可以在javascript中對此文本進行編碼,以免破壞文本。

該文本的路徑為:

- perl => encodes entities on comment text as xml node
- xslt => passes xml text to javascript function (uses disable output encoding which does nothing for this apparently)
- js => handles the text inside the shareSocialWall Function

您的文字似乎已經被弄亂了,可能是錯誤的雙重編碼。

您可以使用正則表達式刪除所有HTML實體:

 var str = 'New Comment on xxxx - WTF, if the dancers don&amp;acirc;&amp;#128;&amp;#153;t come in until 4ish'; console.log(str.replace(/&[^\\s]*;/g, '')); 

請注意,這是非常激進的,如果您的文本編碼確實很差,缺少,可能會刪除比您希望的文本更多的文本; 例如。

暫無
暫無

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

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