2012-12-25 74 views
2

我終於獲得了JsHtmlSanitizer作爲獨立的客戶端腳本工作。 現在我想刪除字符串中的所有HTML標籤,而不僅僅是腳本標籤和鏈接。 這個例子使用JsHtmlSanitizer刪除每個html標記

html_sanitize('<b>hello</b><img src="http://google.com"><a href="javascript:alert(0)"><script src="http://www.google.com"><\/script>'); 

返回「你好」,但我想刪除所有標籤。

+2

在這一點上,爲什麼不把它作爲一個XML文檔並獲得的innerText? –

+0

我想在我正在編寫的程序中使用bb代碼實現實時預覽。因此,首先我要刪除所有的HTML標籤,然後將BB代碼轉換爲HTML –

+1

那麼爲什麼不把它當作XML文檔並獲取innerText呢? –

回答

0

爲什麼不使用正則表達式在清理後刪除所有HTML標記?

var input = '<b>hello</b><img src="http://google.com"><a href="javascript:alert(0)"><script src="http://www.google.com"></script>'; 
var output = null; 
output = html_sanitize(input); 
output = output.replace(/<[^>]+>/g, ''); 

這應該消毒後剝去所有 HTML標籤您輸入的字符串。

如果您只想進行基本的清理(只刪除腳本和樣式標記及其內容和所有html標記),則可以在正則表達式中實現整個事件。我已經在下面演示了一個例子。

var input = '<b>hello</b><img src="http://google.com"><a href="javascript:alert(0)"><script src="http://www.google.com"></script>'; 
input += '<script> if (1 < 2) { alert("This script should be removed!"); } </script><style type="text/css">.cssSelectorShouldBeRemoved > .includingThis { background-color: #FF0000; } </style>'; 

var output = null; 
output = input.replace(/(?:<(?:script|style)[^>]*>[\s\S]+?<\/(?:script|style)[^>]*>)|<[^>]+>/ig, ''); 
+1

難道你不知道有一個殭屍程序可以抓取包含「正則表達式」的答案嗎?和「HTML」,並自動發佈此鏈接作爲評論:http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – user123444555621

0

使用此javascript函數下面從你html_sanitize()得到字符串中刪除所有的HTML標籤。

var output = html_sanitize('<b>hello</b><img src="http://google.com"><a href="javascript:alert(0)"><script src="http://www.google.com"><\/script>'); 

output = output.replace(/(<.*?>)/ig,""); 

希望它能幫助:)