Solved

What are these regex supposed to do?

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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)
Suggested Courses

636 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