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:
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:
/**
* This function replaces XML character entities. '&', '<' and '>' with '&', '<' and '>' respectively.
*/
function sanitize ( str ) {
if ( !str ) {
return str;
}
return str.replace( '&', '&' ).replace( '<', '<').replace( '>', '>');
}
It works. But the question is:
Isn't there any standard function in Javascript to sanitize a string more efficiently or natively? (I'm concerned about Chrome and Firefox)
Note: I've searched StackOverflow and threads like this one are not my answer.