31

I would like to replace all the characters other than 0-9 in a string, using Javascript.

Why would this regex not work ?

 "a100.dfwe".replace(/([^0-9])+/i, "")

6 Answers 6

69

You need the /g modifier to replace every occurrence:

"a100.dfwe".replace(/[^0-9]+/g, "");

I also removed the redundant i modifier and the unused capturing sub-expression braces for you. As others have pointed out, you can also use \D to shorten it even further:

"a100.dfwe".replace(/\D+/g, "");
Sign up to request clarification or add additional context in comments.

5 Comments

@PrakashRaman: it means match one or more of the preceding characters. It's not needed, but it will increase the performance of the regular expression slightly by reducing the number of replaces required.
(Technically this answer, answers the question, but fails on floats) "123.456px".replace(/\D+/g, ""); --> 123456
@AaronSherman that's right—the question did say characters other than 0-9, meaning digits rather than integers or floats. For the latter two, a different, more complicated regex is required, or possibly parseInt() and parseFloat().
@AndyE - parseFloat is what I was looking for when I found this question and couldn't remember that method
as Aaron Shermon points out: "123.456px".replace(/[^0-9.]+/g, "");
24

\D means “not digit”:

"a100.dfwe".replace(/\D/g, "")

Comments

8

What about negative numbers:

Using Andy E's example works unless you have a negative number. Then it removes the '-' symbol also leaving you with an always positive number (which might be ok). However if you want to keep number less than 0 I would suggest the following:

 "a-100.dfwe".replace(/(?!-)[^0-9.]/g, "")    //equals -100

But be careful, because this will leave all '-' symbols, giving you an error if your text looks like "-a-100.dfwe"

1 Comment

He's not really talking about numbers, just groups of digits. Anyway, your regex still matches the - because it's not at the beginning of the string.
2

It doesn't work because the character class [^0-9] (= anything but digits) with the repetition modifier + will only match one sequence of non-numeric characters, such as "aaa".

For your purpose, use the /g modifier as the others suggest (to replace all matches globally), and you could also use the pre-defined character class \D (= not a digit) instead of [^0-9], which would result in the more concise regex /\D+/.

4 Comments

to understand it better shouldn't the "a" and ".dfwe" come under a + group, because that is 1 and more.
\D+ says "look for one or more non-digits", whereas (\D)+ looks for one or more repetitions of the (same) non-digit, which is not what you want. Now, whether you do /\D/g (replace every single non-digit), or /\D+/g (replace every sequence of non-digits) doesn’t really matter except possibly in terms of performance, which won’t be noticeable unless you process tens of thousands of strings.
ah thanks a lot. That explains things perfectly to me. Thanks !
The group has nothing to do with it. [^0-9]+ and ([^0-9])+ will both match the same strings, whether they're aaa or abc. The one with the group just does some unnecessary capturing along the way. The /g flag is the answer.
2
"string@! 134".replace(/[^a-z^0-9]+/g, " ");

this would return "string 134"

Comments

0

Based off of @user3324764's answer - Make a prototype function and convert the number to a number.

String.prototype.extractNumber = function ()
{
    return Number(this.replace(/(?!-)[^0-9.]/g, ""));
};

Example:

"123.456px".extractNumber(); // 123.456

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.