Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

What are these regex supposed to do?

Posted on 2013-11-26
4
Medium Priority
?
193 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 9

Accepted Solution

by:
Derek Jensen earned 2000 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
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…
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…

718 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