开发者

jQuery replace text up to first instance of dollar sign

I have got a number of shipping options and I have cod开发者_StackOverflowe to calculate them. However, I need a way to replace all instances of the words (aka strip out) so i'm just left with the numbers (price).

<select name="ShippingSpeedChoice" onchange="this.form.submit();">
<option value="103">In-store Pickup </option>
<option value="701">UPS Ground $9.90 </option>
<option value="703">UPS 3Day Select® $30.88 </option>
</select>

So I want to strip everything from option value 701 so I am just left with 9.90. I also want the same thing to happen for value 703 so I am just left with 30.88, instead of writing a function for each. Currently I only have a function for option value 701, but I want it dynamic, can it be done..probably with regex which im no good at?

$("option:selected").each(function () { 
var isthePrice = $(this).text().replace("UPS Ground $", ""); }


You want to replace everything (. means any character, * means "match zero or more times") followed by a $ (but in regex the $ is a special character so you have to escape it with the \):

var isthePrice = $(this).text().replace(/.*\$/, '');


This should work for you:

$("option:selected").each(function () { 
    var isthePrice = $(this).text().replace(/^[^$]*/, "");
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜