替换<和>符号<和> HTML字符串中的符号

时间:2016-03-17 08:56:04

标签: regex actionscript-3 actionscript

我想要正则表达式替换<和>符号由& lt;和& gt;在html文本中,并作为有效的XML。

示例字符串:

<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 S

1 个答案:

答案 0 :(得分:2)

应该这样做:

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