Solved

What are these regex supposed to do?

Posted on 2013-11-26
4
186 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Mouse event to control image and transparency. 4 33
why $(this).attr('id'); function not returning value ? 9 52
Jquery 2 23
Asp in server side with Mssql Server 7 4 27
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…
In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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)

839 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