I have a bunch of disorganized information in a var, and I want to:
Loop over the info and extract all the numbers that are preceded with a currency symbol $, or the word Price.
Input all these occurrences into an array
So far I have found a way to find occurrence of the dollar sign, but I have no idea on the rest of the steps I have to take.
var str = "My father/taughtme$500ho<div>wtoPrice:700throwabaseball$30";
var getCount=function(str){
return (str.match(/$/g) || []).length;
};
alert(getCount(str));
Appreciate any help, and sorry if I am not detailed enough.
You can accomplish this with .match
and a regular expression.
var data = "My father/taughtme$500ho<div>wtoPrice:700throwabaseball$30";
var prices = (data.match(/[\$|price:]\d+/gi) || []).map(function(m) {
//Convert each match to a number
return +m.substring(1);
});
document.write(prices);
console.log(prices);
The expression /[\$|price:]\d+/gi
matches all numbers that start with $
or price:
, in any case. Then, use map
to convert each match to a number, and chop off the :
or $
.