Solved

What are these regex supposed to do?

Posted on 2013-11-26
4
182 Views
Last Modified: 2013-12-11
The html includes a span tag with id="productPrice" which wraps around a price with currency symbol, and the in-page javascript includes a variable called baseprice which is set to be the innerHTML of productPrice and therefore includes the currency symbol.

Can you tell me please what does the regex do in the following code?
document.getElementById('productPrice').innerHTML = baseprice.replace(/[+-]*(d+).(d+)/, total.toFixed(2));

Open in new window


The next alternative code is an extended version of the one above; Can you tell me please what does the next regex do?
document.getElementById('productPrice').innerHTML = baseprice.replace(/[+-]*(d+).(d+)/, total.toFixed(2).replace(/(d)(?=(d{3})+.)/g, "$1,"));

Open in new window


The code above does not seem to work, the following does except that it loses the currency symbol:
document.getElementById('productPrice').innerHTML = total.toFixed(2);

Open in new window

0
Comment
Question by:ncw
  • 2
4 Comments
 
LVL 9

Accepted Solution

by:
Derek Jensen earned 500 total points
ID: 39678281
baseprice.replace(/[+-]*(d+).(d+)/, total.toFixed(2));

Open in new window

Okay so what's happening here is within the baseprice var it is:
[+-]*    // Looking for any plus or minus symbols (or possibly both or neither)
(d+)     // then, looking for at least one consecutive digits (and storing it in capture group $1)
.          // then it's looking for any single char (I'm thinking they meant to put \. to look specifically for a period char)
(d+)     // and finally it's looking for another consecutive string of at least one digit (stored in $2)
, total.toFixed(2)    // And, upon finding a match, replacing the entire matched group with total.toFixed(2) -- which also renders the capture groups unnecessary.

baseprice.replace(/[+-]*(d+).(d+)/, total.toFixed(2).replace(/(d)(?=(d{3})+.)/g, "$1,"));

Open in new window

What's happening here is...well no wonder it's not working...

Okay so what the designer was *trying* to accomplish is to find any three consecutive digits and place a comma after it. The only way I was able to accomplish this correctly using regexes was by using two of them, as demonstrated here, although I'm convinced there's an easier way to do it.

There is no regex replace happening here, so of course it would not be inserting any commas :-)
document.getElementById('productPrice').innerHTML = total.toFixed(2);

Open in new window

0
 
LVL 9

Expert Comment

by:Sar1973
ID: 39680335
I suggest you to take a look to W3 schools tutorials (http://www.w3schools.com/js/js_obj_regexp.asp and http://www.w3schools.com/jsref/jsref_obj_regexp.asp). Th ToFixed JS function converts a number to a string, keeping the number of decimals you want (see http://www.w3schools.com/jsref/jsref_tofixed.asp).
0
 
LVL 9

Expert Comment

by:Derek Jensen
ID: 39681026
@ncw wasn't asking what .toFixed() did, he asked what the regexes did, so I explained it. total.toFixed(2) was in the replace argv of .replace(). :-)
0
 
LVL 1

Author Closing Comment

by:ncw
ID: 39711686
Many thanks!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now