1. function sanitizeTextInput(text) {
  2. // Remove HTML tags
  3. const cleanText = text.replace(/<[^>]*>/g, '');
  4. // Remove JavaScript code
  5. const cleanTextNoScript = cleanText.replace(/<script.*?>.*?<\/script>/g, '');
  6. // Remove URLs
  7. const cleanTextNoUrls = cleanTextNoScript.replace(/(https?:\/\/[^\s]+)/g, '');
  8. // Remove email addresses
  9. const cleanTextNoEmails = cleanTextNoUrls.replace(/[\w.-]+@[\w.-]+/, '');
  10. // Remove potentially harmful characters (e.g., control characters)
  11. const cleanTextNoHarmfulChars = cleanTextNoEmails.replace(/[\x00-\x1F\x7F]/g, '');
  12. // Remove extra whitespace
  13. const cleanTextNoExtraWhitespace = cleanTextNoHarmfulChars.replace(/\s+/g, ' ').trim();
  14. //Optional: Remove newlines and tabs
  15. const cleanTextNoNewlines = cleanTextNoExtraWhitespace.replace(/[\n\t]/g, '');
  16. return cleanTextNoNewlines;
  17. }

Add your comment