Wednesday, September 17, 2025
HomeLanguagesJavascriptJavaScript Program to Remove Non-Alphanumeric Characters from a String

JavaScript Program to Remove Non-Alphanumeric Characters from a String

In this article, we will see how to remove non-alphanumeric characters from a string in JavaScript. Non-alphanumeric characters are symbols, punctuation, and whitespace. Removing them from a string This task can be useful when you want to clean up user inputs, sanitize strings, or perform various text processing operations.

There are multiple approaches to removing non-alphanumeric characters from a string in JavaScript.

We will explore all the above methods along with their basic implementation with the help of examples.

Approach 1: Using Regular Expressions

Regular expressions offer a concise way to match and remove non-alphanumeric characters. We can use the replace() method with a regular expression to replace all non-alphanumeric characters with an empty string.

Syntax:

function removeNonAlphanumeric(inputString) {
return inputString.replace(/[^a-zA-Z0-9]/g, '');
};

Example: In this example we are using the above-explained approach.

Javascript




function removeFunction(inputString) {
    return inputString.replace(/[^a-zA-Z0-9]/g, '');
}
  
const originalString = 
    "Hello! This is 123 a test string.";
const result = 
    removeFunction(originalString);
console.log(result);


Output

HelloThisis123ateststring

Approach 2: Using a Loop and Character Checking

By iterating through each character in the string and checking if it’s alphanumeric, we can construct the resulting string without non-alphanumeric characters.

Syntax:

for (let i = 0; i < inputString.length; i++) {
const char = inputString[i];
if (/[a-zA-Z0-9]/.test(char)) {
result += char;
}
};

Example: In this example we are using the above-explained approach.

Javascript




function removeFunction(inputString) {
    let result = '';
    for (let i = 0; i < inputString.length; i++) {
        const char = inputString[i];
        if (/[a-zA-Z0-9]/.test(char)) {
            result += char;
        }
    }
    return result;
}
  
const originalString = 
    "Hello! This is 123 a test string.";
const result = 
    removeFunction(originalString);
console.log(result);


Output

HelloThisis123ateststring

Approach 3: Using the replace() Method with a Custom Function

The replace() method can be used with a custom function that checks each character and replaces non-alphanumeric characters.

Syntax:

function removeNonAlphanumeric(inputString) {
return inputString.replace(/./g, char => {
if (/[a-zA-Z0-9]/.test(char)) {
return char;
}
return '';
});
}

Example: In this example we are using the above-explained approach.

Javascript




function romveFunction(inputString) {
    return inputString.replace(/./g, char => {
        if (/[a-zA-Z0-9]/.test(char)) {
            return char;
        }
        return '';
    });
}
  
const originalString = 
    "Hello! This is 123 a test string.";
const result = 
    romveFunction(originalString);
console.log(result);


Output

HelloThisis123ateststring
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32299 POSTS0 COMMENTS
Milvus
84 POSTS0 COMMENTS
Nango Kala
6660 POSTS0 COMMENTS
Nicole Veronica
11833 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11895 POSTS0 COMMENTS
Shaida Kate Naidoo
6779 POSTS0 COMMENTS
Ted Musemwa
7050 POSTS0 COMMENTS
Thapelo Manthata
6735 POSTS0 COMMENTS
Umr Jansen
6741 POSTS0 COMMENTS