After reading tons of articles and StackOverflow responses I still don't get what is the industry standard approach for dealing with user's inputs (textarea and input type="text"). What and how should I escape chars? What and how should be stripped / replaced so JSON will not break, especially when the user can copy-paste some of the weirdest characters ever?
The doubtful solution that I have put in place a long time ago was:
String.prototype.escapeChars = function () {
return this.replace(/\&/g, '&')
.replace(/\n\r?/g, ' ')
.replace(/\n/g, ' ')
.replace(/\r/g, ' ')
.replace(/\s/g, ' ')
.replace(/\</g, '<')
.replace(/\"/g, '"')
.replace(/\'/g, ''')
.replace(/\>/g, '>')
.replace(/\\/g, ''')
.replace(/[^\x00-\x7F]/g, '');
};
I would tremendously appreciate if someone will explain in detail or point to a good tutorial on how it should be done PROPERLY with vanilla js.