How to remove numbers from string in JavaScript

In JavaScript programming, there are common situations where you need to employ strings and remove specific characters or elements from them.

One of the simple tasks is to remove numbers from a string. Whether you are working with user input, data from an API, or any other source, knowing how to remove numbers from a string in JavaScript can be a valuable skill.

In this article, you will learn the different methods and techniques to perform this task effectively.

Methods to Remove Numbers from String in JavaScript

Here are the following methods to remove numbers from a string in JavaScript.

Method 1: Using Regular Expressions

Regular expressions provide a dynamic way to search for and employ strings in JavaScript.

We can use the replace() method along with a regular expression pattern to remove numbers from a string.

Here’s an example code:

const stringWithNumbersExamples = "remove3421 numbers433 from3672 string123456";
const stringWithoutNumbersExamples = stringWithNumbersExamples.replace(/[0-9]/g, "");
console.log(stringWithoutNumbersExamples);

Output:

remove numbers from string

In this example code, the regular expression pattern /[0-9]/g matches any digit between 0 and 9.

The replace method replaces all circumstance of the matched pattern with an empty string, effectively removing the numbers from the string.

Method 2: Using the Split() and Join()

Another method to remove numbers from a string is by using the split() and join() methods.

Here’s an example code:

const stringWithNumbersExamples = "Hello1234563 World4562134";
const stringWithoutNumbersExamples = stringWithNumbersExamples.split(/[0-9]/).join("");
console.log(stringWithoutNumbersExamples);

Output:

Hello World

Method 3: Using the Replace()

The replace method can be directly used to remove numbers from a string by changing them with an empty string.

Here’s an example code:

const stringWithNumbersExamples = "remove23411 numbers12222 from string222333 in JavaScript22211";
const stringWithoutNumbersExamples = stringWithNumbersExamples.replace(/\d+/g, "");
console.log(stringWithoutNumbersExamples);

Output:

remove numbers from string in JavaScript

In this example code, the regular expression pattern /\d+/g matches one or more consecutive digits.

The replace method replaces all existence of the matched pattern with an empty string.

Method 4: Using a For Loop()

If you prefer a more traditional way, you can use a for loop to iterate over each character in the string and build a new string without the numbers.

Here’s an example code:

const stringWithNumbersExample = "Hello13422223Itsourcecode456456333";
let stringWithoutNumbersExample = "";

for (let x = 0; x < stringWithNumbersExample.length; x++) {
  if (isNaN(parseInt(stringWithNumbersExample[x]))) {
    stringWithoutNumbersExample += stringWithNumbersExample[x];
  }
}

console.log(stringWithoutNumbersExample);

Output:

HelloItsourcecode

In this example code, the for loop iterates over each character in the string.

The isNaN function is used to check if the character is not a number.

If it is not a number, it is appended to the stringWithoutNumbersExample variable.

Method 5: Using the Filter()

The filter method can be used in combination with the isNaN function to filter out the numbers from the string.

Here’s an example code:

const stringWithNumbersExamples = "Welcome23433123ToItsourcecode4234156";
const stringWithoutNumbersExamples = Array.from(stringWithNumbersExamples)
  .filter((char) => isNaN(parseInt(char)))
  .join("");

console.log(stringWithoutNumbersExamples);

Output:

WelcomeToItsourcecode

In this example code, Array.from is used to convert the string into an array of characters.

Then, the filter method is applied to the array to keep only the characters that are not numbers.

Finally, the join method is used to concatenate the filtered characters back into a string.

Method 6: Using the Match()

The match() method can be used along with a regular expression to extract the non-number characters from the string.

Here’s an example code:

const stringWithNumbersExamples = "Hello1285443Itsourcecodes45283326";
const stringWithoutNumbersExamples = stringWithNumbersExamples.match(/[a-zA-Z\s]+/g).join("");
console.log(stringWithoutNumbersExamples);

Output:

HelloItsourcecodes

In this example code, the regular expression pattern /[a-zA-Z\s]+/g matches one or more alphabetic characters (lowercase or uppercase) and whitespace.

The match method returns an array of matched substrings, which is then joined to form the final string without numbers.

Method 7: Using the Trim()

When the string consists of leading or trailing whitespace that you want to eliminate along with the numbers, you can use the trim method in combination with other techniques.

Here’s an example code:

const stringWithNumbersExamples = "Welcome1285443To648444Itsourcecodes45283326";
const stringWithoutNumbersExamples = stringWithNumbersExamples.replace(/\s/g, "").replace(/\d/g, "").trim();
console.log(stringWithoutNumbersExamples);

Output:

WelcomeToItsourcecodes

In this example code, the replace method is used twice. The first replace method removes all whitespace characters using the regular expression pattern /\s/g.

The second replace method removes all digits using the regular expression pattern /\d/g.

Finally, the trim method is called to eliminate any leading or trailing whitespace.

Method 8: Using the Substring()

The substring() method can be used to extract the non-number characters from the string.

Here’s an example code:

const stringWithNumbersJavaScript = "Hello1322223World4545536";
let stringWithoutNumbersJavaScript = "";

for (let x = 0; x < stringWithNumbersJavaScript.length; x++) {
  const char = stringWithNumbersJavaScript[x];
  if (isNaN(parseInt(char))) {
    stringWithoutNumbersJavaScript += char;
  }
}

console.log(stringWithoutNumbersJavaScript); 

Output:

HelloItsourcecode

Method 9: Using the Slice()

The slice() method can be used to extract the non-number characters from the string by splitting it into substrings.

Here’s an example code:


const stringWithNumbersExamples = "Hello123453String456231";
let stringWithoutNumbersExamples = "";

let start = 0;

for (let x = 0; x < stringWithNumbersExamples.length; x++) {
  if (isNaN(parseInt(stringWithNumbersExamples[x]))) {
    if (x > start) {
      stringWithoutNumbersExamples += stringWithNumbersExamples.slice(start, x);
    }
    start = x + 1;
  }
}

if (start < stringWithNumbersExamples.length) {
  stringWithoutNumbersExamples += stringWithNumbersExamples.slice(start);
}

console.log(stringWithoutNumbersExamples);

in this example code, the code removes numbers from a string, storing only the alphabet. It iterates over the string, checking each character.

Alphabets are appended to ‘stringWithoutNumbersExamples ‘.

The final result is printed as ‘123453456231‘.

Method 10: Using a Custom Function

If you choose a reusable method, you can create a custom function to remove numbers from any string in JavaScript.

Here’s an example code:

function removeNumbersFromStringExamples(string) {
  return string.replace(/\d/g, "");
}

const stringWithNumbersExamples = "Hello134223JavaScript42221156";
const stringWithoutNumbersExamples = removeNumbersFromStringExamples(stringWithNumbersExamples);
console.log(stringWithoutNumbersExamples);

Output:

HelloJavaScript

In this example code, the removeNumbersFromStringExamples function takes a string as input and uses the replace method with the regular expression pattern /\d/g to remove all digits from the string.

Method 11: Using the Map()

The map() method can be used to iterate over each character in the string and filter out the numbers.

Here’s an example code:

const stringWithNumbersExamples = "Welcome1232243To4534546JavaScript585844Programming";
const stringWithoutNumbersExamples = Array.from(stringWithNumbersExamples)
  .map((char) => (isNaN(parseInt(char)) ? char : ""))
  .join("");

console.log(stringWithoutNumbersExamples);

Output:

WelcomeToJavaScriptProgramming

In this example code, Array.from is used to convert the string into an array of characters.

The map() method is applied to the array to replace numbers with an empty string.

Finally, the join() method is used to concatenate the mapped characters back into a string.

Method 12: Using the Reduce()

The reduce() method can be used to iterate over each character in the string and build a new string without the numbers.

Here’s an example code:

const stringWithNumbersExamples = "Hello123 To568 the58585 World456 of555755 JavaScript9558 Programming55444";
const stringWithoutNumbersExamples = Array.from(stringWithNumbersExamples).reduce((acc, char) => {
  if (isNaN(parseInt(char))) {
    return acc + char;
  }
  return acc;
}, "");

console.log(stringWithoutNumbersExamples);

Output:

Hello To the World of JavaScript Programming

In this example code, Array.from is used to convert the string into an array of characters.

The reduce() method is then applied to the array, where the accumulator (acc) is updated based on whether the character is a number or not.

The print result is the accumulated string without numbers.

Method 13: Using the Split() and RegExp

The split() method, in combination with a regular expression, can be used to split the string into an array of non-number substrings.

Here’s an example code:

const stringWithNumbersExamples = "Hello1344423World423322256";
const stringWithoutNumbersExamples = stringWithNumbersExamples.split(/\d+/).join("");

console.log(stringWithoutNumbersExamples);

Output:

HelloWorld

In this example code, the regular expression pattern /\d+/ matches one or more consecutive digits.

The split() method is then used to split the string into an array of substrings at the occurrences of the pattern.

Finally, the join() method is used to concatenate the substrings back into a string without numbers.

Method 14: Using the CharCodeAt()

The charCodeAt() method can be used to check the Unicode value of each character in the string and filter out the numbers.

Here’s an example code:

const stringWithNumbersExamples = "How3444 to333 remove35444 numbers78877 from555444 string99900";
const stringWithoutNumbersExamples = Array.from(stringWithNumbersExamples)
  .filter((char) => char.charCodeAt(0) < 48 || char.charCodeAt(0) > 57)
  .join("");

console.log(stringWithoutNumbersExamples);

Output:

How to remove numbers from string

In this example, Array.from is used to change the string into an array of characters.

The filter method is then applied to the array, where the Unicode values of the characters are checked to exclude the numbers.

The join method is used to concatenate the filtered characters back into a string.

Method 15: Using the Concat Method

The concat() method can be used to concatenate the non-number substrings extracted from the string.

Here’s an example code:

const stringWithNumbersExamples = "JavaScript1234563Programming456342Language";
let stringWithoutNumbersExamples = "";

let temporaryStringSamples = "";

for (let i = 0; i < stringWithNumbersExamples.length; i++) {
  const char = stringWithNumbersExamples[i];

  if (isNaN(parseInt(char))) {
    temporaryStringSamples += char;
  } else {
    if (temporaryStringSamples) {
      stringWithoutNumbersExamples = stringWithoutNumbersExamples.concat(temporaryStringSamples);
      temporaryStringSamples = "";
    }
  }
}

if (temporaryStringSamples) {
  stringWithoutNumbersExamples = stringWithoutNumbersExamples.concat(temporaryStringSamples);
}

console.log(stringWithoutNumbersExamples);

Output:

JavaScriptProgrammingLanguage

In this example code, the for loop iterates over each character in the string.

If the character is not a number, it is added to the temporaryString.

When a number is encountered, the temporaryString is concatenated with the stringWithoutNumbersExamples, and the temporaryString is reset.

Finally, if there is any that remains temporaryString, it is concatenated with the stringWithoutNumbersExamples.

Method 16: Using the IsNaN() Function

The isNaN() function can be used to filter out the numbers from the string.

Here’s an example code:

const stringWithNumbersExamples = "Hello1245443World45634221";
const stringWithoutNumbersExamples = Array.from(stringWithNumbersExamples)
  .filter((char) => !isNaN(parseInt(char)))
  .join("");

console.log(stringWithoutNumbersExamples);

In this example code, Array.from is used to convert the string into an array of characters.

The filter method is then applied to the array, where the isNaN() function is used to remove the numbers.

The join method is used to concatenate the filtered characters back into a string.

Method 17: Using Regular Expressions and the Replace Method

Regular expressions can be connected to the replace method to remove numbers from the string.

Here’s an example code:

const stringWithNumbersExamples = "javaScript12356443Programming456677855";
const stringWithoutNumbersExamples = stringWithNumbersExamples.replace(/[0-9]/g, "");

console.log(stringWithoutNumbersExamples);

Output:

javaScriptProgramming

Method 18: Using the Test Method

The test method can be used with a regular expression to check if a character is a number or not.

Here’s an example code:

const stringWithNumbersExamples = "Hello12345363World45643222";
let stringWithoutNumbersExamples = "";

for (let x = 0; x < stringWithNumbersExamples.length; x++) {
  const char = stringWithNumbersExamples[x];

  if (!/\d/.test(char)) {
    stringWithoutNumbersExamples += char;
  }
}

console.log(stringWithoutNumbersExamples);

In this example code, the for loop iterates over each character in the string.

The test method is used with the regular expression /d/ to check if the character is a digit.

If it is not a digit, it is appended to the stringWithoutNumbersExamples variable.

Method 19: Using the Push and Join

The push() method can be used to add non-number characters to an array, which can be joined to form the final string.

Here’s an example code:

const stringWithNumbersExamples = "Hello12334222World45645322";
const stringWithoutNumbersExamples = [];

for (let x = 0; x < stringWithNumbersExamples.length; x++) {
  const char = stringWithNumbersExamples[x];

  if (isNaN(parseInt(char))) {
    stringWithoutNumbersExamples.push(char);
  }
}

console.log(stringWithoutNumbersExamples.join("")); 

Output:

In this example code, the for loop iterates over each character in the string.

The isNaN() function is used to check if the character is not a number.

If it is not a number, it is added to the stringWithoutNumbersExamples array using the push method.

Finally, the join method is used to concatenate the array elements into a string.

Method 20: Using the RegExp Test

The test method can be used with a regular expression to filter out the numbers from the string.

Here’s an example code:

const stringWithNumbersExamples = "Hello1243333World45553332256";
const stringWithoutNumbersExamples = Array.from(stringWithNumbersExamples)
  .filter((char) => !/\d/.test(char))
  .join("");

console.log(stringWithoutNumbersExamples);

In this example, Array.from is used to convert the string into an array of characters.

The filter() method is utilized in the array, where the test method is used with the regular expression /d/ to remove the numbers.

The join() method is used to concatenate the filtered characters back into a string.

FAQs

How can I remove numbers from a string in JavaScript?

There are few ways to remove numbers from a string in JavaScript.

You can use methods like replace(), split() and join(), filter(), match(), or substring().

Each method has its own advantages and can be used based on your specific requirements.

Can I use regular expressions to remove numbers from a string in JavaScript?

Yes, regular expressions are a powerful tool for pattern matching in strings. You can use regular expressions with methods like replace or test to remove numbers from a string.

Can I remove numbers from a string without using regular expressions in JavaScript?

Yes, you can remove numbers from a string without using regular expressions in JavaScript.

Methods like split() and join(), filter(), match(), substring(), or iterating over the string with a loop and checking each character can be used to achieve the desired result.

Conclusion

In this article, we have explored different methods to remove numbers from a string in JavaScript.

Whether you are working with user input, manipulating data, or performing string operations, it is important to have the ability to remove numbers from strings when needed.

We enclosed the methods like replace, split and join, filter, match, substring, and more.

Additional Resources

Leave a Comment