In the following:
<span>This cost $15.99 per item</span>
how do I return "$15.99" using jquery? The value can also be for example "$7".
In the following:
<span>This cost $15.99 per item</span>
how do I return "$15.99" using jquery? The value can also be for example "$7".
$("span").filter(function() { return this.text().match('\$\d+(.\d+)?'); });
You can use just the match of match('\$\d+(\.\d+)?')
, but the above function will filter spans that contain the match.
The expression would be something similar to \$\d+(\.\d+)?
or \$\d+(?:\.\d+)?
to get rid of the sub group.
Will the text always be "This cost [your price] per item", or is the text also arbitrary?
If the text is fixed, you can just $.replace()
everything except the price (in two steps) with an empty string.