Replace all non alphanumeric characters, new lines, and multiple white space with one space
Categories:
Mastering String Cleaning: Replacing Non-Alphanumeric Characters and Whitespace

Learn how to effectively clean strings in JavaScript by replacing non-alphanumeric characters, newlines, and consolidating multiple spaces into a single space using regular expressions.
Cleaning and sanitizing strings is a common task in web development. Whether you're processing user input, preparing data for storage, or formatting text for display, you often need to remove unwanted characters, normalize whitespace, and ensure consistency. This article will guide you through using JavaScript's powerful regular expressions to achieve a clean string that contains only alphanumeric characters and single spaces.
Understanding the Problem: Messy Strings
Imagine you receive a string from a user or an external API that looks something like this:
Hello, World!\nThis is a test. 123@abc
This string contains:
- Leading and trailing spaces.
- Multiple spaces between words.
- Punctuation marks (
,
,!
,.
,@
). - Newline characters (
\n
).
Our goal is to transform this into a clean string like: Hello World This is a test 123 abc
.
flowchart TD A[Input String] --> B{"Remove Non-Alphanumeric (except space)"} B --> C{"Remove Newlines"} C --> D{"Replace Multiple Spaces with Single Space"} D --> E[Cleaned String]
Flowchart illustrating the string cleaning process.
Step 1: Removing Non-Alphanumeric Characters and Newlines
The first step involves targeting all characters that are not alphanumeric (letters A-Z, a-z, and numbers 0-9) and all newline characters. Regular expressions provide a concise way to do this.
We'll use the replace()
method with a regular expression. The regex /[^a-zA-Z0-9\s]/g
is a good starting point:
[^...]
is a negated character set, meaning it matches any character not in the set.a-zA-Z0-9
matches all uppercase and lowercase letters and all digits.\s
matches any whitespace character (spaces, tabs, newlines, etc.).- The
g
flag ensures that all occurrences are replaced, not just the first one.
However, if we want to preserve spaces, we need a slightly different approach. A more direct way to remove non-alphanumeric characters and newlines, while temporarily keeping all spaces (including multiple ones), is to target [^a-zA-Z0-9\s]
and then handle newlines separately, or combine them carefully. A simpler regex to remove everything that is not alphanumeric or a space is /[^a-zA-Z0-9 ]/g
.
const messyString = " Hello, World!\nThis is a test. 123@abc ";
// Regex to match anything that is NOT an alphanumeric character or a space
const cleanedStep1 = messyString.replace(/[^a-zA-Z0-9 ]/g, '');
console.log(cleanedStep1);
// Expected output: " Hello World This is a test 123 abc "
Removing non-alphanumeric characters (excluding spaces) from a string.
\s
character class includes newlines, tabs, and spaces. If you want to preserve only spaces and remove newlines, you might need a two-step approach or a more complex regex. For this specific problem, [^a-zA-Z0-9 ]
is effective for removing non-alphanumeric characters while keeping all spaces (including newlines which are now treated as spaces).Step 2: Consolidating Multiple Spaces
After the first step, you might have multiple spaces between words or at the beginning/end of the string. The next step is to replace any sequence of one or more whitespace characters with a single space. We'll use the regex /\s+/g
for this:
\s
matches any whitespace character.+
is a quantifier that means "one or more" of the preceding character.- The
g
flag ensures all occurrences are replaced.
Finally, we'll use trim()
to remove any leading or trailing spaces that might remain.
const messyString = " Hello, World!\nThis is a test. 123@abc ";
// Step 1: Remove non-alphanumeric characters (keeping all spaces)
const tempCleaned = messyString.replace(/[^a-zA-Z0-9 ]/g, '');
// Step 2: Replace multiple spaces with a single space and trim
const finalCleaned = tempCleaned.replace(/\s+/g, ' ').trim();
console.log(finalCleaned);
// Expected output: "Hello World This is a test 123 abc"
Combining regex and trim()
for a fully cleaned string.
Putting It All Together: A Single Function
For convenience, you can encapsulate these two operations into a single function that takes a string and returns its cleaned version.
function cleanString(inputString) {
// 1. Replace all non-alphanumeric characters (except space) with an empty string
// This also effectively removes newlines as they are non-alphanumeric.
const withoutSpecialChars = inputString.replace(/[^a-zA-Z0-9 ]/g, '');
// 2. Replace any sequence of one or more whitespace characters with a single space
// and then trim leading/trailing spaces.
const finalCleaned = withoutSpecialChars.replace(/\s+/g, ' ').trim();
return finalCleaned;
}
const testString1 = " Hello, World!\nThis is a test. 123@abc ";
const testString2 = "Another_Example-With-Symbols!\nAnd\tTabs.";
console.log(`Original 1: "${testString1}"`);
console.log(`Cleaned 1: "${cleanString(testString1)}"`);
console.log(`Original 2: "${testString2}"`);
console.log(`Cleaned 2: "${cleanString(testString2)}"`);
// Expected output:
// Original 1: " Hello, World!\nThis is a test. 123@abc "
// Cleaned 1: "Hello World This is a test 123 abc"
// Original 2: "Another_Example-With-Symbols!\nAnd\tTabs."
// Cleaned 2: "Another Example With Symbols And Tabs"
A reusable JavaScript function for comprehensive string cleaning.