Reputation: 65
Here is the script;:
var output = []
output = this.getField("SelectedSoftware").value.match(/\$(\d{1,4}\.\d\d)/g);
Example text from the field:
Automate, Audition ($500.00), Citrix Receiver, AutoCAD ($54.93), Clarity Studio ($748.23), Audacity ($300.00), Audition ($500.00), Business Objects Dashboard, Audition ($500.00),
The problem I have is getting just the cost numbers so they can be added together to display a total. (?<=\$)
doesn't seem to work. so I grouped the rest of the expression in Parentheses. However, I'm not sure how to get those group values instead of the full matches.
Upvotes: 2
Views: 50
Reputation: 163
RegExp.exec(string)
should be really helpful. In fact, it's the only way to obtain capture groups from a string. The implementation is a bit tough though, since .exec()
only returns a single result.
Sample code:
var regex = /\$(\d{1,4}\.\d\d)/g,
text = this.getField("SelectedSoftware").value,
prices = [],
total = 0,
num;
// Assignments always return the value that is assigned.
// .exec will return null if no matches are found.
while((val = regex.exec(text)) !== null){
// Capture groups are referenced with 'val[x]', where x is the capture group number.
num = parseFloat(val[1]);
total += num;
prices.push(num);
}
This works because RegExp
objects have a property called .lastIndex
which it bases the next .exec()
search on.
You can read more about .exec()
on the MDN.
Upvotes: 1
Reputation: 23816
I have is getting just the cost numbers so they can be added together to display a total. String.prototype.match always returns an array as you want.
See this complete DEMO
var output = []
output = "Automate, Audition ($500.00), "
+"Citrix Receiver, AutoCAD ($54.93), "+
+"Clarity Studio ($748.23), Audacity "
+"($300.00), Audition ($500.00), Business "
+"Objects Dashboard, Audition ($500.00),"
.match(/\$(\d{1,4}\.\d\d)/g);
Output:
["$500.00", "$54.93", "$748.23", "$300.00", "$500.00", "$500.00"]
Now you can simply add all number using This regex /(\d{1,4}\.\d\d)/g
.
Getting sum of these values:
var total = 0;
for(var _=0; _<output.length; _++)
total += parseFloat(output[_]);
See OUTPUT 2603.16
Upvotes: 0
Reputation: 6768
You can use (?: )
to create non-capturing groups, but I don't think you need it in this case.
MDN: Working with regular expressions
exec: A RegExp method that executes a search for a match in a string. It returns an array of information.
test: A RegExp method that tests for a match in a string. It returns true or false.
match: A String method that executes a search for a match in a string. It returns an array of information or null on a mismatch.
search: A String method that tests for a match in a string. It returns the index of the match, or -1 if the search fails.
replace: A String method that executes a search for a match in a string, and replaces the matched substring with a replacement substring.
split: A String method that uses a regular expression or a fixed string to break a string into an array of substrings.
What you need is RegExp.exec, which will return an array of information form which you can get the captured group.
It returns an array with the whole match in [0]
, then each captured group in the following array slots. So what you want is:
var price = /\$(\d{1,4}\.\d\d)/.exec("$59.99")[1];
Upvotes: 0