XML sanitization and reverse satinization in JavaScript
<p>This is probably a very simple question but it's several hours that I'm searching for a satisfying answer and haven't found any:</p>

<p>I am getting a string from the user but I want to sanitize and show it in an alert. The best I could come up with is to write the following function:</p>

<pre><code>/**
 * This function replaces XML character entities. '&amp;', '&lt;' and '&gt;' with '&amp;amp;', '&amp;lt;' and '&amp;gt;' respectively.
 */
function sanitize ( str ) {
    if ( !str ) {
        return str;
    }
    return str.replace( '&amp;', '&amp;amp;' ).replace( '&lt;', '&amp;lt;').replace( '&gt;', '&amp;gt;');
}
</code></pre>

<p>It works. But the question is:</p>

<p><strong>Isn't there any standard function in Javascript to sanitize a string more efficiently or natively?</strong> (I'm concerned about Chrome and Firefox)</p>

<p>Note: I've searched StackOverflow and threads like <a href="http://stackoverflow.com/questions/9164164/how-to-escape-or-replace-in-xml-file-using-jquery">this one</a> are not my answer.</p>
