Open In App

How to Remove Punctuation from Text using JavaScript ?

In this article, 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 geeksforgeeks

Below are the following approaches through which we can remove punctuation from text using JavaScript:

Approach 1: Using replace() Method with Regular Expression

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 geeksforgeeks!!!";
console.log(remove(str)); 

Output
Welcome to the geeksforgeeks

Approach 2: Using for loop

In this approach we will use for loop to iterate over the string and make the function for checking the punctuation. If the character is punctuation marks then it will not add in result variable and if it is not then we concat it to the result variable.

function remove(str) {
    let res = '';
    for (let i = 0; i < str.length; i++) {
        let character = str.charAt(i);
        if (!checkPunctuation(character)) {
            res += character;
        }
    }
    return res;
}

function checkPunctuation(char) {
    const punctuation = '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~';
    return punctuation.includes(char);
}
let str = "Welcome, to the geeksforgeeks!!!";
console.log(remove(str));

Output
Welcome to the geeksforgeeks

Approach 3: Using split, filter, and join

In this approach we are using split, filter, and join in JavaScript removes punctuation from a string.We start by splitting the string into an array of characters, then filtering out characters that are not letters, numbers, or spaces using a, and finally we will join the filtered array back into a string.

Example: In this example we defines a function to remove punctuation from a string using split, filter, and join methods. It cleans the text by keeping only alphanumeric characters and spaces.

function removePunctuation(str) {
    return str.split('').filter(char => {
        return /[a-zA-Z0-9 ]/.test(char);
    }).join('');
}

let text = "GeeksforGeeks a computer science portal.";
let cleanedText = removePunctuation(text);
console.log(cleanedText);

Output
GeeksforGeeks a computer science portal

Approach 4: Using ASCII Values

In this approach we will iterate over each character in text and checks if it is punctuation based on its ASCII value. If it is punctuation ew exclude it otherwise we append that character to result.

Example: In this example we defines a function to remove punctuation from a string using ASCII Values. It cleans the text by keeping only alphanumeric characters and spaces.

function removePunctuation(text) {
    let result = "";
    for (let i = 0; i < text.length; i++) {
        let charCode = text.charCodeAt(i);
        // Check if the character is not a punctuation character (33-47, 58-64, 91-96, 123-126 in ASCII)
        if ((charCode < 33 || charCode > 47) &&
            (charCode < 58 || charCode > 64) &&
            (charCode < 91 || charCode > 96) &&
            (charCode < 123 || charCode > 126)) {
            result += text[i];
        }
    }
    return result;
}
let text = "Hello, Geek. Welcome to GFG!";
let cleanedText = removePunctuation(text);
console.log(cleanedText);

Output
Hello Geek Welcome to GFG
Article Tags :