Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing Numbers from a String using Javascript

How do I remove numbers from a string using Javascript?

I am not very good with regex at all but I think I can use with replace to achieve the above?

It would actually be great if there was something JQuery offered already to do this?

//Something Like this??  var string = 'All23'; string.replace('REGEX', ''); 

I appreciate any help on this.

like image 633
Abs Avatar asked May 20 '10 22:05

Abs


People also ask

How do I remove numbers from a string in Java?

We can remove numbers from a String java by using replaceAll( ) method, regex and charAt( ) method.

How do you cut a string in Javascript?

The slice() method extracts a part of a string. The slice() method returns the extracted part in a new string. The slice() method does not change the original string. The start and end parameters specifies the part of the string to extract.


1 Answers

\d matches any number, so you want to replace them with an empty string:

string.replace(/\d+/g, '') 

I've used the + modifier here so that it will match all adjacent numbers in one go, and hence require less replacing. The g at the end is a flag which means "global" and it means that it will replace ALL matches it finds, not just the first one.

like image 109
nickf Avatar answered Oct 06 '22 12:10

nickf