Navigate back to the homepage

how to remove special characters and space from number in javascript?

Infinitbility
JavaScript
July 13th, 2022 · 1 min read
how to remove special characters and space from number in javascript?

Hi Friends 👋,

Welcome To Infinitbility! ❤️

To remove special characters and space from number in javascript, just use replace() method with /[ `[email protected]#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?~]/g regex then it will return new string without special characters and spaces then convert it number.

The replace() method returns a new string with some or all matches of a pattern replaced by a replacement. The pattern can be a string or a RegExp, and the replacement can be a string or a function called for each match. If pattern is a string, only the first occurrence will be replaced.

Let’s take an example to remove special characters and space using replace() method.

1const regex = /[ `[email protected]#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?~]/g;
2
3var str = "12 3&&9 04";
4
5// + unary plus oprator it's the fastest way to convert a string to a number
6+str.replace(regex, '');
7// 👇️ Output
8// 123904

Today, I’m going to show you How do I remove special characters and spaces from numbers in javascript, as above mentioned here, I’m going to use the replace() method with /[ `[email protected]#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?~]/g regex to delete special characters and space from a string and then convert it to number.

Let’s start today’s tutorial how do you remove special characters and space from numbers in javascript?

In this example, we will do

  1. take sample number string variable
  2. remove special characters and space
  3. convert it to a number

Let’s write code…

javascript replace example

1const regex = /[ `[email protected]#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?~]/g;
2
3// take sample number string variable
4let num = "235 && 243";
5
6// remove special characters and space
7num = num.replace(regex, '');
8
9// convert it to number
10num = +num;
11
12console.log(num)

As mentioned above, we are taken the example of a number string variable, remove special characters and spaces from the string, converted the string to a number, and print the output on the screen.

I hope it helps you, All the best 👍.

JavaScript, remove special characters and space from number example
JavaScript, remove special characters and space from number example

Follow me on Twitter

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo Photoshop Guide

Tutorials

Categories

More articles from Infinitbility

how to remove special characters and space from string in javascript?

how to remove special characters and space from string in javascript?

An example of delete special characters and space in javascript with step by step guide.

July 13th, 2022 · 1 min read
how to remove special characters from string in react js?

how to remove special characters from string in react js?

An example of delete special characters from string in react js with step by step guide.

July 12th, 2022 · 1 min read
© 2020–2022 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]