开发者

How to hide everything after a dollar sign in a String JQuery

开发者 https://www.devze.com 2023-04-07 00:42 出处:网络
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?

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.

0

精彩评论

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

关注公众号