reg exp to replace non-numeric characters?

L

laredotornado

Hi,

I'm trying to replace all non-numeric characters with nothing in my
variable. However, this doesn't seem to work ...

var myMixedVar = myMixedVar.replace(/^[0-9]/, "");

Any suggestions what I should have as the reg exp instead?

Thanks, - Dave
 
D

d d

I'm trying to replace all non-numeric characters with nothing in my
variable. However, this doesn't seem to work ...

var myMixedVar = myMixedVar.replace(/^[0-9]/, "");

I'm no expert on regular expressions, but don't you want the ^ inside
the square brackets, and then a g after the / to make it global.

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

~dd
 
S

Sudrien

I'm trying to replace all non-numeric characters with nothing in my
variable. However, this doesn't seem to work ...
var myMixedVar = myMixedVar.replace(/^[0-9]/, "");

I'm no expert on regular expressions, but don't you want the ^ inside
the square brackets, and then a g after the / to make it global.

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

~dd



^[blah] : b, l, a, or h as the first character in the line
[^blah] : can not match b, l, a, or h


from http://www.javascriptkit.com/jsref/regexp.shtml:
\D Match any non-digit. Equivalent to [^0-9].

so it could also be written

..replace(/\D/g,"");

-Sud.
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]
oglegroups.com>, Fri, 13 Jul 2007 14:25:26, "(e-mail address removed)"
I'm trying to replace all non-numeric characters with nothing in my
variable. However, this doesn't seem to work ...

var myMixedVar = myMixedVar.replace(/^[0-9]/, "");

Any suggestions what I should have as the reg exp instead?

/\D/g

See references via <URL:http://www.merlyn.demon.co.uk/js-valid.htm> or
the link at the one instance of "regexp" in the FAQ.

<FAQENTRY> To that FAQ link should be added one to a similar reference
including newer features; or a new question should be added to introduce
an answer including a set of RegExp references.

It's a good idea to read the newsgroup c.l.j and its FAQ. See below.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top