function normalizeWhitespace(text) { // Replaces multiple whitespace characters (space, tab, newline, etc.) with a single space. // Then trims leading/trailing spaces. return text.replace(/\s+/g, ' ').trim(); } const messyText = " Hello world! This is a test. "; console.log(normalizeWhitespace(messyText)); // Output: "Hello world! This is a test." const singleSpaceText = "Just a regular sentence."; console.log(normalizeWhitespace(singleSpaceText)); // Output: "Just a regular sentence."