473,322 Members | 1,188 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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 1338
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Lord Khaos | last post by:
If I am trying to find an expression, foo, I can do something like this: rExp = /foo/gi; if(results.search(rExp) > -1){ and all work fine. however, if I want my search term to be a...
3
by: Jon Maz | last post by:
Hi All, Am getting frustrated trying to port the following (pretty simple) function to CSharp. The problem is that I'm lousy at Regular Expressions.... //from...
6
by: Vishant | last post by:
Hi, I'm new to javascript and regEx and trying to solve the following problem. I have a function which validates the password if there is a number:...
3
by: Chris | last post by:
I need to write a function that will remove a specified parameter from a URL. For example: removeParam("param1", "http://mysite.com/mypage.htm?param1=1&param2=2"); would return: ...
2
by: Mr.Clean | last post by:
I am working on modifying a syntax highlighter written in javascript and it uses several regexes. I need to add a language to the avail highlighters and need the following regexes modified to...
0
by: Chris McKenzie | last post by:
Hi, I'm using IE 6, and I'm doing some RegEx replacement on the client. Here's my code: regExStr = "\\d{2}:\\d{2}"; // where pattern matches ##:## regExStr += "(?=\\s)"; // and the next...
13
by: The Cleaning Wonder Boy | last post by:
Could someone please explain to me what the (?<Key> and (?<Value> are in the following Regex expression? This gets relative links in an HTML string (file). ...
3
by: jab3 | last post by:
Hello. I"m new to this group, and to JavaScript in general, so please forgive me if I breach local etiquette. I'm trying to implement some client-side 'dynamic' validation on a form. I'm having...
2
by: shapper | last post by:
Hello, I am trying to validate a value using Regex. How can I do this with Javascript? Thanks, Miguel
9
by: kummu4help | last post by:
can anyone give me a regex to validate the password with following conditions hope i am clear. i tried with ctype_alnum() function in php but it is accepting if all characters or either...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.