2015-06-29 14 views
5

各種可能性我有一個問題:約window.location.href.replace

window.location.href.replace 

是否有可能將其應用於一組2分或更多的話嗎?

例子:

window.location.href = window.location.href.replace("-standard", "-standard-upload"); 

我需要與其他2更換2個字,在這個例子中我改變1到另一個1.

+0

你的意思是'' - 標準''不止一次出現在原始字符串中,還是你想要替換完全不同的單詞?請顯示輸入「href」和相應的所需輸出示例。 – nnnnnn

+0

實施例: www.substellar.it/tiket/stardard/this-is-example 必須成爲 www.substellar.it/upload/stardard/another-words-changed與window.location.href.replace –

+0

你可以鏈接'.replace()'的多個用途,比如:'x = x.replace(「a」,「b」)。replace(「c」,「d」);'。 – nnnnnn

回答

3

可能是你可以這樣做:

var a = window.location.href; 
a = a.replace("-standard", "-standard-upload"); 
a = a.replace("old", "new"); 
window.location.href = a; 
+0

我測試它但不工作。 –

+1

爲什麼最後一行需要'.replace()'?只要做一個分配'... href = a;'。 – nnnnnn

+0

@nnnnnn對不起,謝謝! ':)' –

2
str = window.location.href; 
str.replace("this", "another").replace("is", "words").replace("example", "changed") 

作爲一個通用的例子...

+0

window.location.href = window.location.href.replace(「 - standard」,「-standard-upload」);但我需要更換另外2個字:上傳類別 –

+0

您沒有正確解釋這一點。你可以請嘗試再解釋一下,我可以幫助你。 – midda25

+0

實施例: www.substellar.it/tiket/stardard/this-is-example 必須成爲 www.substellar.it/upload/stardard/another-words-changed –