开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜