Solved

What are these regex supposed to do?

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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Add Clickable Links to Google Map Markers 3 29
REGEX HELP 11 43
Passport Expiry 3 46
Call a function within the ASP code 4 12
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

803 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