Javascript Replace?

B

Brad

I see the use of Javascript replace all over the web. What are all the
character sequences? (sorry I am a bit of a newbie at this).

i.value.replace(/[^\d]+/g, '');

I understand that /g is global and /i is case sensitive, but what are
the rest? I am asking because I am trying to write a function that
takes an input and replaces everything but numbers and a . (for decimal
numbers).

Thanks in advance for your help.

~Brad
 
P

petermichaux

Hi Brad,

Between the two forward slashes is the meat of the regular expression.

[^\d]+

[^ ] is match any character that is not between the brackets
\d is a digit 0-9
+ means one or more occurances
From your description I think you might want to add "\." to make sure
you don't replace the decimal points.

i.value.replace(/[^\d\.]+/g, '');

but this will jam all the number together because the non numbers are
being replaced with nothing. This might be better.

i.value.replace(/[^\d\.]+/g, ' ');


Peter
 
B

Brad

Peter,

Thank you for the information. I appreciate it. In acutality, I want
the numbers to be jammed together. I am using this for a numeric only
field.
 
D

Dr John Stockton

JRS: In article <[email protected]>
, dated Thu, 13 Apr 2006 12:31:05 remote, seen in
From your description I think you might want to add "\." to make sure
you don't replace the decimal points.

i.value.replace(/[^\d\.]+/g, '');

but this will jam all the number together because the non numbers are
being replaced with nothing. This might be better.

i.value.replace(/[^\d\.]+/g, ' ');

It does, however, break up numbers containing non-whitespace thousands
separators.
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top