字符串替换不起作用

时间:2016-08-24 07:49:57

标签: javascript jquery

我有一个字符串:

var bag_overweight_bag = "More Than 23 kg (50 lbs.), but not exceeding 45 kg (100 lbs.) - $50 - $59 USD/CAD";

这里我需要替换GBP而不是CAD。 我的代码是:

var baggage_currency = "GBP";
bag_overweight_bag = bag_overweight_bag.replace(/CAD/g, baggage_currency);

上面的代码无法正常工作,但如果我提供下面的替换代码就可以了

bag_overweight_bag = bag_overweight_bag.replace(/CAD/g, 'GBP');

那么问题是什么?

2 个答案:

答案 0 :(得分:0)

  

代码片段工作正常,请确保您在执行替换时不会在任何地方覆盖baggage_currency

答案 1 :(得分:-1)

  

这是你的代码。我已经用bag替换了bag_overweight_bag   overweight_bag。

var overweight_bag = "More Than 23 kg (50 lbs.), but not exceeding 45 kg (100 lbs.) - $50 - $59 USD/CAD";

var baggage_currency = "GBP"; 

var bag_overweight_bag1 = overweight_bag.replace(/CAD/g, baggage_currency);


console.log(bag_overweight_bag1)