How to usereplace() Method with Regular Expression in Javascript
In this approach, we will use the replace() method with a regular expression and replace punctuations with an empty string.
Example:
function remove(str) {
return str.replace(/[!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~]/g, '');
}
let str = "Welcome, to the w3wiki!!!";
console.log(remove(str));
Output
Welcome to the w3wiki
How to Remove Punctuation from Text using JavaScript ?
We will learn how to remove punctuation from text using JavaScript. Given a string, remove the punctuation from the string if the given character is a punctuation character, as classified by the current C locale. The default C locale classifies these characters as punctuation:
! " # $ % & ' ( ) * + , - . / : ; ? @ [ \ ] ^ _ ` { | } ~
Examples:
Input : %welcome' to @geeksforgeek<s
Output : welcome to w3wiki
Below are the following approaches through which we can remove punctuation from text using JavaScript:
Table of Content
- Approach 1: Using replace() Method with Regular Expression
- Approach 2: Using for loop
- Approach 3: Using split, filter, and join
- Approach 4: Using ASCII Values
- Approach 5: Using filter() Method with an Array
- Approach 6: Using Array.from() and Array.filter()