Remove all non-alphanumeric characters from a string in JavaScript
This post will discuss how to remove all non-alphanumeric characters from a string in JavaScript.
Non-alphanumeric characters are any characters that are not letters, digits, or underscores, such as spaces, punctuation marks, or symbols. Removing them from a string can be useful for cleaning up user inputs, sanitizing strings, or performing various text processing operations. Here are some of the most common ways to remove non-alphanumeric characters from a string, along with some examples and explanations:
1. Using replace() function
The replace() function searches for a specified pattern within a string and replaces it with a new substring. We can use a regular expression to match any non-alphanumeric characters we want to remove and replace them with an empty string. For example, to remove all non-alphanumeric characters from a string, we can use the following code. It uses the regular expression /[^a-zA-Z0-9]/g which matches any character that is not in the range of a to z, A to Z, or 0 to 9. The flag g means global, which means find all matches in the string, not just the first one.
|
1 2 3 4 5 6 7 8 9 10 |
// The string with non-alphanumeric characters let str = "Hello, User1!"; // The regular expression to match non-alphanumeric characters let nonAlphanumeric = /[^a-zA-Z0-9]/g; // Use the replace function to remove the non-alphanumeric characters let modifiedString = str.replace(nonAlphanumeric, ""); console.log(modifiedString); // Output: "HelloUser1" |
The replace() function can also take a function as the second argument, instead of a string. The function will be called for each match in the string, and return the replacement value. We can use this feature to create a custom function that checks each character and replaces non-alphanumeric characters with an empty string. For instance:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
// The string with non-alphanumeric characters let str = "Hello, User1!"; // The string without non-alphanumeric characters let modifiedString = str.replace(/./g, function(char) { if (/[a-zA-Z0-9]/.test(char)) { // If the character is alphanumeric return char; // Return it as it is } return ""; // Otherwise, return an empty string }); console.log(modifiedString); // Output: "HelloUser1" |
The code uses the replace() function with a regular expression and a function as arguments. The regular expression is ., which means any single character except line terminators. The function takes one parameter, which is the current match (character), and returns either the same character or an empty string, depending on whether it’s alphanumeric or not. The function uses the same regular expression as before to check if a character is alphanumeric or not.
2. Using a loop and a character set
We can use a loop to iterate through each character in a string and check if it belongs to a set of alphanumeric characters. If it does, we can add it to a new string. This function is simple and can be used in many programming languages, including JavaScript. For example, to remove all non-alphanumeric characters from a string:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
// The string with non-alphanumeric characters let str = "Hello, User1!"; // The set of alphanumeric characters let alphanumeric = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; // The new string to store the modified string let modifiedString = ""; // Loop through each character in the original string for (let i = 0; i < str.length; i++) { // Check if the character belongs to the alphanumeric set if (alphanumeric.includes(str[i])) { // Add the character to the new string modifiedString += str[i]; } } console.log(modifiedString); // Output: "HelloUser1" |
That’s all about removing non-alphanumeric characters from a string in JavaScript.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)