2009-12-04 20 views
1

我不知道今天我的腦袋在哪裏,有人請向我解釋 - 爲什麼我可以通過請求表單獲取hturl值並在htstring中進行替換? (我這樣做,它的工作 - 但只取代(hturl)值的一個發生....問題是,我想要替換的舊值約有10多個發生:使用javascript在Regexp.replace中的動態值

我正在使用正則表達式結合ASP和javascript

<% 
htreplace = ""+Request.Form("1")+""; 
hturl = Request.Form("thesite"); 
htstring = htreplace 
htstring = htstring.replace(/,;~~~~/ig,';'); 
htstring = htstring.replace(hturl,'http://www.example.net'); 
%> 

如果我改變

htstring = htstring.replace(hturl,'http://www.example.net'); 

htstring = htstring.replace(/hturl/ig,'http://www.example.net'); 

htstring = htstring.replace("/"+hturl+"/"+ig,'http://www.example.net'); 

我無法動態地獲得hturl值嗎?

任何人都可以幫助請做! - 我不知道今天我的頭/邏輯在哪!謝謝

回答

4
var myregexp = new RegExp(hturl, "ig"); 

htstring = htstring.replace(myregexp, 'blah'); 
+0

謝謝! - 剛剛學到了一些新東西,它可以100%實現零問題! – 2009-12-04 11:42:18

+1

不客氣:) – 2009-12-04 11:53:43

+0

很棒的幫助,正是我需要的! Thanx much – TJB 2009-12-16 22:29:56