ToddN
ToddN

Reputation: 2961

jQuery replace text up to first instance of dollar sign

I have got a number of shipping options and I have code 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 $", ""); }

Upvotes: 0

Views: 1125

Answers (2)

k.parnell
k.parnell

Reputation: 2787

This should work for you:

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

Upvotes: 0

Sean Bright
Sean Bright

Reputation: 120644

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(/.*\$/, '');

Upvotes: 2

Related Questions