Remove All Non-Alphabetic Characters from a String in JavaScript



Non-alphabetic characters are any characters that are not part of the alphabet, and the strings are a data type that represents a sequence of characters or text. Working with strings is a basic task in JavaScript, and one common task is removing all non-alphabetic characters from a string. In this article, we will understand how to remove all non-alphabetic characters from a string.

Understanding the Problem

Suppose we have a string saying "he@656llo wor?ld". We want to remove all digits, punctuation, whitespace, and special characters from the given string. For example:

Input string we have:

"he@656llo wor?ld"

And the Output we want:

"hello world"

Removing Non Alphabetic Characters From String

We can remove the Non Alphabetic Characters from a string in the following ways:

Using replace() with Regular Expression

Regular expressions are the most efficient way of removing non-alphabetic characters from a string with the replace() method. The replace() method is a string method that searches for a specified substring or a regular expression pattern within a string and replaces it with a new substring.

Example

In the following example, we have defined a function called remove Nonalphabetic that uses regular expressions with the replace method. This function takes the string, replaces all non-alphabetic characters from it, and returns it.

// defining function for removing non-alphabetic characters from string
function removeNonAlphabetic(str) {
    return str.replace(/[^A-Za-z]/g, '');
}

//Calling function and printing result in the console
console.log(removeNonAlphabetic("Hello, World! 123"));

Output

HelloWorld

Using For Loop

Using a for..of loop with character checking is another simple method of removing non-alphabetic characters from a string. The string match() method searches a string or a regular expression in the original string and returns an array with all matches. This method works efficiently for character checking.

Example

In the above example, we have used a regular expression with the replace method, but in this example, we are using a for-of loop and the string match() method that does character checking and returns the matched character. In this way, we get a string without non-alphabetic characters.

// defining function for removing non-alphabetic characters from string
function removeNonAlphabetic(str) {
    let result = '';
    for (let char of str) {
        if (char.match(/[a-zA-Z]/)) {
            result += char;
        }
    }
    return result;
}
//Calling function and printing result in the console
console.log(removeNonAlphabetic("Hello, World! 123")); 

Output

HelloWorld

Using Array Methods with string conversions

Using array methods such as the filter() method and the join() method while converting a string into an array using the string split() method is another simple and efficient method of removing non-alphabetic characters from a string.

The filter() method of an array returns a new array of elements that pass the specified conditions, and the join method of an array joins the array elements and converts them into a string.

Example

In this example, we have defined a function called remove Nonalphabetic that firstly converts the string into an array using the split() method, then uses array methods on it such as the filter() method, and again recreates the string using join() and returns it.

// defining function for removing non-alphabetic characters from string
function removeNonAlphabetic(str) {
        return str.split('').filter(char => /[A-Za-z]/.test(char)).join('');
}
//Calling function and printing result in the console
console.log(removeNonAlphabetic("Hello, World! 123"));

Output

HelloWorld

Conclusion

You can easily remove non-alphabetic characters from a string in JavaScript by using the above-mentioned methods. In this article, we have seen simple and efficient methods for removing non-alphabetic characters from a string. Understanding these methods helps the developer to choose the best way to do this.

Updated on: 2025-03-18T12:59:47+05:30

658 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements