Posted By

adrianparr on 12/17/10


Tagged

regex replace xml regexp name encode entity regular Expression names chars special characters as3


Versions (?)

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


 / Published in: ActionScript 3
 

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

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.

  1. // http://xml.silmaril.ie/authors/specials/
  2.  
  3. function replaceSpecialXmlChars($str:String):String {
  4. var regExp:RegExp;
  5. regExp = /&/g;
  6. $str = $str.replace(regExp, "&");
  7. regExp = /</g;
  8. $str = $str.replace(regExp, "&lt;");
  9. regExp = />/g;
  10. $str = $str.replace(regExp, "&gt;");
  11. regExp = /"/g;
  12. $str = $str.replace(regExp, "&quot;");
  13. regExp = /'/g;
  14. $str = $str.replace(regExp, "&apos;");
  15. return $str;
  16. }
  17.  
  18. var myStr:String = "<&>\"'<&>\"'";
  19. trace(myStr);
  20. trace(replaceSpecialXmlChars(myStr));
  21.  
  22. // OUTPUT
  23. // <&>"'
  24. // &lt;&amp;&gt;&quot;&apos;

Report this snippet  

You need to login to post a comment.