2016-03-17 158 views
-1

我想用正則表達式代替<和>符號由& lt;和& gt;在HTML文本中,並作爲有效的XML。將< and >符號替換爲HTML字符串中的<和>符號

示例串:

<HTML><BODY><p style="margin:0px">His <span style="color: rgb(237, 23, 23);">career </span>spanned the development of cinema, from <span style="font-weight: 700;">silent film</span>, through early experiments in <span style="text-transform: uppercase;">Technicolor </span>to filmmaking in the 21st century.​</p><p>His career spanned the development of cinema, from silent film, through early experiments in Technicolor to filmmaking in the 21st century.<This sample></p>​</BODY></HTML> 

結果:

<HTML><BODY><p style="margin:0px">His <span style="color: rgb(237, 23, 23);">career </span>spanned the development of cinema, from <span style="font-weight: 700;">silent film</span>, through early experiments in <span style="text-transform: uppercase;">Technicolor </span>to filmmaking in the 21st century.​</p><p>His career spanned the development of cinema, from silent film, through early experiments in Technicolor to filmmaking in the 21st century.&lt;This sample&gt;</p>​</BODY></HTML> 

感謝

Iyyappan小號

+0

您使用哪種語言?如果你使用PHP,你可以使用'htmlentities'來編碼所有特殊字符。 – Barmar

+0

我在flex中使用動作腳本3.0。 –

+0

https://stackoverflow.com/q/2342453/570812 – Passerby

回答

0

應該這樣做:

private function replaceTags(string:String):String 
{ 
    string = string.replace(/</gi, "&lt;"); 
    string = string.replace(/>/gi, "&gt;"); 

    return string; 
} 

trace("Replaced string: " + replaceTags("String <with>")); 

另一種選擇是將您的文本以XML格式打包成CDATA

+0

這也將取代html標籤。我只想替換不包含在Flex支持的HTML標記中的< and >符號。 –

+0

噢,這是一個挑戰:)如果你把整個HTML部分包裝到CDATA中,並把它傳遞給XML時不會有什麼幫助 - 不需要修改任何東西? – Philarmon

+0

我想將整個HTML部分轉換成XML。 –

相關問題