JAVASCRIPT

Cleaning and Sanitizing Text Input with Regex

Effectively clean and sanitize user input by removing extra spaces, HTML tags, or unwanted characters using JavaScript regular expressions.

const sanitizeText = (text) => {
  // 1. Remove leading/trailing whitespace
  let cleanedText = text.trim();

  // 2. Replace multiple spaces with a single space
  cleanedText = cleanedText.replace(/\s+/g, ' ');

  // 3. (Optional) Remove basic HTML tags (be cautious, use a parser for robust sanitization)
  cleanedText = cleanedText.replace(/<\/?[^>]+(>|$)/g, "");

  // 4. (Optional) Remove specific non-alphanumeric characters (keep spaces)
  // cleanedText = cleanedText.replace(/[^a-zA-Z0-9\s]/g, "");

  return cleanedText;
};

const dirtyInput = "  Hello  <b>World</b>!   This is a   test.  ";
console.log(sanitizeText(dirtyInput)); // "Hello World! This is a test."
How it works: This JavaScript function `sanitizeText` demonstrates how to clean and sanitize text input using regular expressions. It first trims leading/trailing whitespace, then collapses multiple internal spaces into single spaces. Optionally, it shows how to remove basic HTML tags and specific non-alphanumeric characters, providing a cleaner, safer string for display or further processing.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs