Reputation: 6315
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".
Upvotes: 2
Views: 3293
Reputation: 34347
$("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.
Upvotes: 1
Reputation: 9661
The expression would be something similar to \$\d+(\.\d+)?
or \$\d+(?:\.\d+)?
to get rid of the sub group.
Upvotes: 2
Reputation: 65264
alert($('span').text().match(/\$\d+.\d+/));
but there is a code here for the regex to be more precise...
Upvotes: 0
Reputation: 60584
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.
Upvotes: 0