f



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

0
dougawells (13)
4/6/2006 1:06:18 AM
comp.lang.javascript 38370 articles. 0 followers. javascript4 (1315) is leader. Post Follow

4 Replies
1594 Views

Similar Articles

[PageSpeed] 35

dougawells@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
0
Thomas
4/6/2006 2:02:21 AM
Try:

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

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

dougawells@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

0
Kam
4/6/2006 2:03:53 AM
dougawells@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>
0
RobG
4/6/2006 2:10:26 AM
JRS:  In article <mM_Yf.974$Nv4.135999@news.optus.net.au>, dated Thu, 6
Apr 2006 02:10:26 remote, seen in news:comp.lang.javascript, RobG
<rgqld@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.
0
Dr
4/6/2006 10:27:14 PM
Reply: