function sanitizeTextInput(text) {
// Remove HTML tags
const cleanText = text.replace(/<[^>]*>/g, '');
// Remove JavaScript code
const cleanTextNoScript = cleanText.replace(/<script.*?>.*?<\/script>/g, '');
// Remove URLs
const cleanTextNoUrls = cleanTextNoScript.replace(/(https?:\/\/[^\s]+)/g, '');
// Remove email addresses
const cleanTextNoEmails = cleanTextNoUrls.replace(/[\w.-]+@[\w.-]+/, '');
// Remove potentially harmful characters (e.g., control characters)
const cleanTextNoHarmfulChars = cleanTextNoEmails.replace(/[\x00-\x1F\x7F]/g, '');
// Remove extra whitespace
const cleanTextNoExtraWhitespace = cleanTextNoHarmfulChars.replace(/\s+/g, ' ').trim();
//Optional: Remove newlines and tabs
const cleanTextNoNewlines = cleanTextNoExtraWhitespace.replace(/[\n\t]/g, '');
return cleanTextNoNewlines;
}
Add your comment