"$75.00" into "75.00" JQuery or Javascri

"$75.00" into "75.00" JQuery or Javascript

本文关键字:quot Javascri or JQuery into      更新时间:2023-09-26

我试图简单地从字符串中删除"$"。

我用这个得到价格:

var currentPrice = $.trim($("#ProductPrice").text());

我只是不确定从那里做什么。

这应该可以做到:

currentPrice = currentPrice.replace("$", "");

只是用.replace.

currentPrice = currentPrice.replace(/'$/, '');

如果这是标准格式,并且您不想使用正则表达式,则可以使用slice()

$("#ProductPrice").text().slice(1);

在这里摆弄

有一个简单的方法使用 .replace

var newCurrentPrice = currentPrice.replace("$", ""); // value = 75.00

如果您确定字符串的格式始终是 $75.00 ,请尝试使用 .substring()

currentPrice = currentPrice.substring(1);

下面是有关.substring()方法的更多信息。

你可以用"无"代替美元。

currentPrice = currentPrice.replace(/'$/g, '');

$替换为空字符串,''

var currentPrice = $.trim($("#ProductPrice").text()).replace("$", "");