问答 百科手机端

asp和js的字符串替换函数Replace

2023-05-20 17:28

javascript:

var str="...xyz..."; //假设要把xyz替换成abc

str.replace(/xyz/g/i,"abc")

其中/g(多次替换),/i(不区分大小写)

注意第一个参数没有引号。

 

Asp vbscript:

title=replace(title,"DF","SD",1,-1,1)
replace函数参数详解:
参数1:源字符串
参数2:要被替换的字符
参数3:新的字符
参数4:值为1,指定从第一个字符开始搜索该字符串
参数5:值为-1 指定每一个子串都要被替换
参数6:值为1 指定字符串的比较不区分大小写。

asp使用正则表达式的replace

Set regEx = New RegExp
regEx.Pattern = reg  '正则表达式
regEx.IgnoreCase = True '是否忽略大小写
regEx.Global = True
newStr= regEx.Replace(oldStr,newPart) 'reg被替换成newPart
Set regEx = Nothing

Replace替换忽略大小写

Replace(expression, find, replacewith[, start[, count[, compare]]])   原理同上,count是指替换次数,-1为全部替换。


 

热门