By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,837 Members | 1,665 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,837 IT Pros & Developers. It's quick & easy.

Javascript Replace?

P: n/a
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

Apr 13 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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

Apr 13 '06 #2

P: n/a
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.

Apr 13 '06 #3

P: n/a
JRS: In article <11**********************@g10g2000cwb.googlegroups .com>
, dated Thu, 13 Apr 2006 12:31:05 remote, seen in
news:comp.lang.javascript, pe**********@gmail.com posted :
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.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Apr 14 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.