469,343 Members | 5,350 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,343 developers. It's quick & easy.

regex and Javascript

Hi -
I've never tried using regular expressions with JS. Can someone help me
with a simple one?

I have a field that a user inputs that looks for an integer, or a some
integers with a decimal then does some calculations and converts it to
currency. Problem is, that if i have to recalculate, that field now has
a "$" in it which throws off calculations. I'd like to look at the
field and if there is a "$", strip it out, do the calculations, etc.

So, something like:

function getDollar() {

myAmount = window.document.form.amount.value;

//here is where i want to look for the $ sign...

Thanks,
Doug

Apr 6 '06 #1
4 1288
do********@gmail.com wrote:
I have a field that a user inputs that looks for an integer, or a some
integers with a decimal then does some calculations and converts it to
currency. Problem is, that if i have to recalculate, that field now has
a "$" in it which throws off calculations. I'd like to look at the
field and if there is a "$", strip it out, do the calculations, etc.


myAmount = window.document.form.amount.value.replace("$", "");

You are about the 4711st person to ask this here. There are more
sophisticated solutions available, but your question was not precise
enough.
PointedEars
Apr 6 '06 #2
Try:

myAmount.replace(/^\$/, "")

--
Kam-Hung Soh
http://kamhungsoh.blogspot.com - It Mostly Works
http://members.optusnet.com.au/khsoh - Software That Mostly Works

do********@gmail.com wrote:
Hi -
I've never tried using regular expressions with JS. Can someone help me
with a simple one?

I have a field that a user inputs that looks for an integer, or a some
integers with a decimal then does some calculations and converts it to
currency. Problem is, that if i have to recalculate, that field now has
a "$" in it which throws off calculations. I'd like to look at the
field and if there is a "$", strip it out, do the calculations, etc.

So, something like:

function getDollar() {

myAmount = window.document.form.amount.value;

//here is where i want to look for the $ sign...

Thanks,
Doug


Apr 6 '06 #3
do********@gmail.com said on 06/04/2006 11:06 AM AEST:
Hi -
I've never tried using regular expressions with JS. Can someone help me
with a simple one?
There is a tutorial here:

<URL:http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Guide:Regular_Expressions>
There are many others, regular expressions are a very big field. Be
careful of tutorials based on other languages - regular expressions tend
to be very similar in different languages and platforms but there are
differences here and there.

I have a field that a user inputs that looks for an integer, or a some
integers with a decimal then does some calculations and converts it to
currency. Problem is, that if i have to recalculate, that field now has
a "$" in it which throws off calculations. I'd like to look at the
field and if there is a "$", strip it out, do the calculations, etc.

So, something like:

function getDollar() {

myAmount = window.document.form.amount.value;

//here is where i want to look for the $ sign...


Further to Thomas' reply, which may well be sufficient and does not use
a regular expression, to remove the $ only if it's at the start of the
string:

myAmount = window.document.form.amount.value.replace(/^\$/,'');
To remove all occurrences of $:

myAmount = window.document.form.amount.value.replace(/\$/g,'');
To remove anything that is not a digit or period (.):

myAmount = window.document.form.amount.value.replace(/[^\d\.]/g,'')
As a rule, keep all your values as numbers (possibly integer cents is
best if you are working with money) and only format them for humans or
data exchange.
--
Rob
Group FAQ: <URL:http://www.jibbering.com/FAQ>
Apr 6 '06 #4
JRS: In article <mM******************@news.optus.net.au>, dated Thu, 6
Apr 2006 02:10:26 remote, seen in news:comp.lang.javascript, RobG
<rg***@iinet.net.au> posted :

To remove anything that is not a digit or period (.):

myAmount = window.document.form.amount.value.replace(/[^\d\.]/g,'')
As a rule, keep all your values as numbers (possibly integer cents is
best if you are working with money) and only format them for humans or
data exchange.


With that in mind, I'd consider making myAmount of type Number (OP: FAQ
4.21) :

myAmount = + window.document.form.amount.value.replace(/[^\d\.]/g, "")
--
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 6 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by Lord Khaos | last post: by
6 posts views Thread by Vishant | last post: by
3 posts views Thread by Chris | last post: by
2 posts views Thread by Mr.Clean | last post: by
reply views Thread by Chris McKenzie | last post: by
13 posts views Thread by The Cleaning Wonder Boy | last post: by
2 posts views Thread by shapper | last post: by
1 post views Thread by CARIGAR | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.