亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

ASP(VBS)中Replace問題

ASP(VBS)中Replace問題

弒天下 2022-02-25 15:22:59
在JS,Replace可以將$n(SubMatches)傳遞給Function,如:var s = "Cuilu$Test$007"function test(str){if(str == "Test"){return "True"}else{return "False"}}var news = s.replace(/\$(\w)+\$/ig,test("$1"))//news的值將是s中的$Test$被替換成True后的CuiluTrue007而在ASP(VBS)中,如dim s:s = "Cuilu$Test$007"function test(str)if str = "Test" thentest = "True"elsetest = "False"end functiondim reg:set reg = regExpreg.Pattern = "\$(\w+)\$"dim news:news = reg.replace(s,test("$1"))'news的值將是s中的$Test$被替換成False后的CuiluTrue007我分別獲取了一下JS和VBS中Function接收到的Arguments其中JS接到到的Argument為 Test而VBS中function接收到的則是$1很明顯VBS將$1做為普通字符串傳遞了,而JS中則是SubMatches對象中第一次匹配到的傳遞請高手提示一下,怎么在ASP的Replace中,也讓$n為匹配到的SubMatch傳遞給替換Function的參數!
查看完整描述

2 回答

?
瀟瀟雨雨

TA貢獻1833條經驗 獲得超4個贊

正確代碼如下:
'═════代═══碼═══開═══始═════
dim s
s = "Cuilu$Test$007"
function test(str)
if str = "Test" then
test = "True"
else
test = "False"
end if
end function
dim reg
set reg = new regExp
reg.Pattern = "\$(\w+)\$"
Set Matches=reg.Execute(s)
Set oMatch = Matches(0)
'這一個0表示第一個匹配項$test$,vbs中正則比較低級,不能直接識別括號
data=oMatch.SubMatches(0)
'這一個0表示括號的數據test
dim news
news = reg.replace(s,test(data))
msgbox news
'═════代═══碼═══結═══束═════

注意這是vbs文件的寫法,在asp中有一句話可能不是這樣寫(set reg = new regExp,你改回你的寫法就是,我這樣給你是方便你測試,建個vbs文件即可運行看到結果)



查看完整回答
反對 回復 2022-02-28
?
楊魅力

TA貢獻1811條經驗 獲得超6個贊

Function test(matchStr, str, matchPos, sourceStr)
if str = "Test" then
test = "True"
else
test = "False"
End Function
news = reg.replace(s,getRef("test"))
--------------
如果有兩個子匹配組,那么Function的參數就變成5個。
3個,參數就是6個,依次類推。
這里的str是第一個子匹配組。



查看完整回答
反對 回復 2022-02-28
  • 2 回答
  • 0 關注
  • 278 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號