While working with strings in JavaScript, we might come across situations where we need to remove special characters except space from a string. To remove special characters from a String in JavaScript, we have to use regular expressions with the String.prototype.replace() method.
In this article, we will learn how to remove all special characters except space from a string in JavaScript.
Table of Contents:
Removing Special Characters except Space Using Regex
Regular Expressions(Regex) in JavaScript are used to search, find, and replace strings based on specific patterns. We can also use regex to remove special characters from a string while keeping space. We have to use the replace() function to replace all the special characters with empty strings.
Code:
function removeSpecialCharacters(str) {
return str.replace(/[^a-zA-Z0-9 ]/g, '');
}
let inputString = "Welc#$%^&ome to Int&*()ellipa@@@at";
let result = removeSpecialCharacters(inputString);
console.log(result);
Output:
Welcome to Intellipaat
Explanation:
- str.replace(/[^a-zA-Z0-9 ]/g, ”): This regular expression matches any character that is not a lowercase letter (a – z), uppercase letter (A – Z), digit (0 – 9), or space.”
- The g flag is used to make sure that all the special characters are replaced with an empty string.
Removing Special Characters except Space Using match()
Another method to remove special characters from a string is using match() function and joining the filtered characters to get the answer.
Code:
function removeSpecialCharactersUsingMatch(str) {
return (str.match(/[a-zA-Z0-9 ]/g) || []).join('');
}
let inputString = "Intelli@#$%paat is awesom$%^&*()e!";
let result = removeSpecialCharactersUsingMatch(inputString);
console.log(result);
Output:
Intellipaat is awesome
Explanation:
- match(/[a-zA-Z0-9 ]/g): This method filters all valid characters (letters, numbers, and spaces).
- || []: This line makes sure that we don’t get null if no match is found.
- .join(”): This function joins the filtered characters into a new string.
Conclusion
So far in this article, we have learned how we can remove all special characters except space from a string using regular expressions in JavaScript. We have to use replace() function and match() to do this task. If you want to learn more about web development, you can check out Intellipaat’s Web Development Course to become a web developer expert.
FAQs
1. How to remove space from a string in JavaScript?
To remove space from a string in JavaScript, you can use the .replace() method with a regular expression:
Code:
let str = "Hello World";
let result = str.replace(/\s+/g, '');
console.log(result);
Output:
HelloWorld
2. How to remove specific characters from a string in JavaScript?
To remove specific characters from a string, you can use the .replace() method with a regular expression.
Code:
let str = "Hello, World!";
let result = str.replace(/[,!]/g, '');
console.log(result);
Output:
Hello World
3. How to give space in a string in JavaScript?
We can add space in a string using concatenation or template literals.
Code:
let str1 = "Hello";
let str2 = "World";
let result = str1 + " " + str2;
console.log(result);
let result2 = `${str1} ${str2}`;
console.log(result2);
Output:
Hello World
Hello World
4. What is trim() in JavaScript?
trim() function in JavaScript is used to remove whitespace from both the beginning and end of a string.