2011-03-30 97 views
0

只是漫游将有可能在jQuery中部分字符串替换?在jquery部分字符串替换

我尝试使用下面的代码,但是这似乎并不为我工作:

var test = "testing_supplyAddress_001"; 
test.replace('supplyAddress', 'billingAddress'); 

我试图只更换supplyAddress到billingAddress所以输出将testing_billingAddress _001

+0

这与jQuery无关。这是纯粹的JavaScript。 – Ender 2011-03-30 23:53:36

+0

这是jQuery不可能的,因为它不提供任何字符串操作函数。但是你可以像你一样使用基本的JavaScript。 – 2011-03-30 23:55:41

回答

2

的JavaScript字符串是静态的,因此.replace()实际上并不是修改为的字符串。你需要,可以指定由.replace()函数返回到变量的值:

var test = "testing_supplyAddress_001"; 
test = test.replace('supplyAddress', 'billingAddress'); 

Here's a demo showing this in action ->

2

它工作正常。它并没有替代它 - replace()方法返回一个新的字符串。

var test = "testing_supplyAddress_001"; 
var newTest = test.replace('supplyAddress', 'billingAddress'); 
alert(newTest); 
0

这只是普通的旧javascript - 但也可以用于jQuery。

var test = "testing_supplyAddress_001".replace('supplyAddress', 'billingAddress');