Remove all punctuations from a string in JavaScript
This post will discuss how to remove all punctuations from a string in JavaScript.
Punctuation marks are any characters that are not letters, digits, or whitespace, such as commas, periods, question marks, exclamation points, etc. To remove punctuation marks from a string in JavaScript, we need to use some function or function that can find and replace the punctuation patterns in the string. Here are some of the most common and easy-to-use functions for removing punctuation marks:
1. Using replace() function
This is a simple and straightforward way to remove punctuation marks from a string by using the replace() function, which returns a new string with the matched values replaced without changing the original string. The replace() function takes two arguments: a pattern to match and a replacement value. The pattern can be a string or a regular expression that defines the punctuation pattern.
For example, the following code uses the regular expression /[.,\/#!$%\^&\*;:{}=\-_`~()]/g to remove punctuation marks from a string. It matches any character that is in the brackets globally (g), meaning it will find all occurrences in the string. The replacement value is an empty string (""), which will replace each matched character with nothing.
|
1 2 3 4 5 6 7 8 9 10 11 |
// The original string let originalString = "Hello, World!"; // The regular expression to match punctuation marks let punctuation = /[.,\/#!$%\^&\*;:{}=\-_`~()]/g; // Use the replace function to remove the punctuation marks let modifiedString = originalString.replace(punctuation, ""); // Log the modified string to the console console.log(modifiedString); // Output: "Hello World" |
2. Using split() and join() functions
By using the split() and join() functions, we can also remove punctuation marks from a string. The split() function breaks a string into smaller pieces based on a separator, and the join() function puts the pieces back together into a new string using a separator. The separator can be a string or a regular expression that defines the punctuation pattern. For example, if we want to remove punctuation marks from the string "Hello, world!", we can write:
|
1 2 3 4 5 6 7 8 9 |
let originalString = "Hello, World!"; // The regular expression to match punctuation marks let punctuation = /[.,\/#!$%\^&\*;:{}=\-_`~()]/g; // Use the split and join function to remove the punctuation marks let modifiedString = originalString.split(punctuation).join(""); console.log(modifiedString); // Output: "Hello World" |
The split() function will return an array of substrings that are separated by the punctuation pattern, such as ["Hello", "world", ""]. The join() function will return a string that is composed of the array elements joined by an empty string ("").
3. Using a custom function
If we want more control over how to remove punctuation from a string, we can write our own function that iterates over each character and checks if it is a punctuation character. If it is, we can skip it or replace it with another character. If it is not, we can append it to a new string. The following function illustrates this:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
let removePunctuation = (string) => { // The character set to match punctuation marks let punctuation = "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"; // create an empty string let result = ""; // Loop over each character in the string for (let c of string) { // Check if the character is not a punctuation mark using regular expression if (!punctuation.includes(c)) { // append the character to the result string result += c; } } return result; }; // The original string let originalString = "Hello, World!"; // Use the replace function to remove the punctuation marks let modifiedString = removePunctuation(originalString); // Log the modified string to the console console.log(modifiedString); // Output: "Hello World" |
That’s all about removing all punctuations 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 :)