Return to Snippet

Revision: 37829
at December 18, 2010 06:19 by adrianparr


Updated Code
// http://xml.silmaril.ie/authors/specials/

function replaceSpecialXmlChars($str:String):String {
	var regExp:RegExp;
	regExp = /&/g;
	$str = $str.replace(regExp, "&");
	regExp = /</g;
	$str = $str.replace(regExp, "&lt;");
	regExp = />/g;
	$str = $str.replace(regExp, "&gt;");
	regExp = /"/g;
	$str = $str.replace(regExp, "&quot;");
	regExp = /'/g;
	$str = $str.replace(regExp, "&apos;");
	return $str;
}

var myStr:String = "<&>\"'<&>\"'";
trace(myStr);
trace(replaceSpecialXmlChars(myStr));

// OUTPUT
// <&>"'
// &lt;&amp;&gt;&quot;&apos;

Revision: 37828
at December 18, 2010 06:19 by adrianparr


Updated Code
// http://xml.silmaril.ie/authors/specials/

function replaceSpecialXmlChars($str:String):String {
	var regExp:RegExp;
	regExp = /&/g;
	$str = $str.replace(regExp, "&amp;");
	regExp = /</g;
	$str = $str.replace(regExp, "&lt;");
	regExp = />/g;
	$str = $str.replace(regExp, "&gt;");
	regExp = /\"/g;
	$str = $str.replace(regExp, "&quot;");
	regExp = /'/g;
	$str = $str.replace(regExp, "&apos;");
	return $str;
}

var myStr:String = "<&>\"'<&>\"'";
trace(myStr);
trace(replaceSpecialXmlChars(myStr));

// OUTPUT
// <&>"'
// &lt;&amp;&gt;&quot;&apos;

Revision: 37827
at December 18, 2010 06:17 by adrianparr


Updated Code
// http://xml.silmaril.ie/authors/specials/

function replaceSpecialXmlChars($str:String):String {
	var regExp:RegExp;
	regExp = /&/g;
	$str = $str.replace(regExp, "&amp;");
	regExp = /</g;
	$str = $str.replace(regExp, "&lt;");
	regExp = />/g;
	$str = $str.replace(regExp, "&gt;");
	regExp = /"/g;
	$str = $str.replace(regExp, "&quot;");
	regExp = /'/g;
	$str = $str.replace(regExp, "&apos;");
	return $str;
}

var myStr:String = "<&>\"'<&>\"'";
trace(myStr);
trace(replaceSpecialXmlChars(myStr));

// OUTPUT
// <&>"'
// &lt;&amp;&gt;&quot;&apos;

Revision: 37826
at December 17, 2010 23:00 by adrianparr


Initial Code
// http://xml.silmaril.ie/authors/specials/

function replaceSpecialXmlChars($str:String):String {
	$str = $str.replace("&", "&amp;");
	$str = $str.replace("<", "&lt;");
	$str = $str.replace(">", "&gt;");
	$str = $str.replace("\"", "&quot;");
	$str = $str.replace("'", "&apos;");
	return $str;
}

var myStr:String = "<&>\"'";
trace(myStr);
trace(replaceSpecialXmlChars(myStr));

// OUTPUT
// <&>"'
// &lt;&amp;&gt;&quot;&apos;

Initial URL
http://xml.silmaril.ie/authors/specials/

Initial Description
These five characters should be replaced with their HTML entity names before being used in XML, otherwise they may cause the XML to be invalid.

Initial Title
AS3 Replace Special XML Characters with Entity Names (Partial Set)

Initial Tags
regex, replace, xml, regexp

Initial Language
ActionScript 3