I have a string like this:
string="Awesome Option +$399.99";
I want to remove the +$399.99 (and any other pricing details) from the string, how would you do that?
Oh and the string could also possibly look like this:
开发者_如何学编程string="#4 Cool Option +$23.99"
var text = 'Awesome Option +$399.99';
text = text.substring(0, text.indexOf('+$'));
// text is "Awesome Option "
You would do: string.replace(/\s*\+\$.*$/, '')
. Note that this does not depend on jQuery.
This also removes white space in front of the +$
. If the +
is optionally there, you'd use string.replace(/\s*\+?\$.*$/, '')
.
var originalString = "Awesome Option +$399.99"
var truncatedString = originalString.split("+")[0]
Using the same logic you could extract just the price this way, or both at the same time.
精彩评论