2011-03-30 96 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');