vbs或js,正则轮翻在文章段落后加上网址,网站名称,网站介绍等内容

yongfa365 2007-02-28 01:13:54
Function RegExpTest(strng,patrn,patrn2)
Dim regEx, Match, Matches ' 建立变量。
Set regEx = New RegExp ' 建立正则表达式。
regEx.IgnoreCase = True ' 设置是否区分字符大小写。
regEx.Global = True ' 设置全局可用性。
patrn=split(patrn,"|")
patrn2=split(patrn2,"|")

For Each p in patrn
regEx.Pattern = p ' 设置模式。
Set Matches = regEx.Execute(strng) ' 执行搜索。
For Each Match in Matches ' 遍历匹配集合。
Randomize
strng = Replace(strng, Match.Value, Match.Value & patrn2(CInt(Ubound(patrn2)*Rnd)))
Next

RegExpTest = strng
Next
End Function

MsgBox(RegExpTest( "asdfas1111111df<br>asdddd222222dddd</p>asdf3333asdf<br>asddd444444444ddddd</p>asdfas555555df<Br>asdddddd66dd</P>","<br>|</p>","www.baidu.com|www.yongfa365.com|www.sian.com"))


函数没写成功,请高手指点
...全文
246 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
yongfa365 2007-02-28
  • 打赏
  • 举报
回复
感谢各位,终于成功了,思想来源与
superwfei(文盲)
把下边代码存为.vbs可以运行可能看到效果

Function RegExpTest(strng, patrn, patrn2)
Dim regEx, Match, Matches ' 建立变量。
Set regEx = New RegExp ' 建立正则表达式。
regEx.IgnoreCase = True ' 设置是否区分字符大小写。
regEx.Global = True ' 设置全局可用性。
patrn = Split(patrn, "|")

For Each p in patrn
regEx.Pattern = p ' 设置模式。
strng = regEx.Replace(strng, p & "||" & Chr(10))
Next

strng = Split(strng, "||")
For Each E in strng
s = s + E + arrArt(patrn2)
Next
RegExpTest = s
End Function

Function arrArt(patrn2)
arrArti = Split(patrn2, "|")
Randomize
arrArt = arrArti(CInt(UBound(arrArti) * Rnd))
End Function

MsgBox(RegExpTest( "111<br/>222</p>333<br/>444</p>555<Br>666</P>111", "<br/>|</p>|<br>", "www.baidu.com|www.yongfa365.com|www.sina.com"))
文盲老顾 2007-02-28
  • 打赏
  • 举报
回复
Function ExpStr1(vstr,reg)
Dim re,Matchs,item,tstr
tstr=""
Set re=New RegExp
re.Pattern=reg
re.IgnoreCase=True
re.Global=True
If re.test(vstr) Then
Set Matchs=re.execute(vstr)
For Each item In Matchs
tstr=tstr&item.value&","&item.FirstIndex&"||"
Next
tstr=Split(Left(tstr,Len(tstr)-2),"||")
End If
Set re=Nothing
ExpStr1=tstr
End Function

Function formatOut(vstr,vsp,vurl)
vtmp=vstr
turl=Split(vurl,"|")
tstr=ExpStr1(vstr,vsp)
For i=UBound(tstr) To 0 Step -1
vnum=Split(tstr(i),",")
vtmp=Left(vtmp,vnum(1))&vnum(0)&turl(Int(UBound(turl)*Rnd))&Right(vtmp,Len(vtmp)-vnum(1)-Len(vnum(0)))
' response.write "<li>"&server.htmlencode(tstr(i))
Next
formatOut=vtmp
End Function

Randomize
tmp="asdfas1111111df<br/>asdddd222222dddd</p>asdf3333asdf<br/>asddd444444444ddddd</p>asdfas555555df<Br>asdddddd66dd</P>"
stmp="<br/>|</p>"
utmp="www.baidu.com|www.yongfa365.com|www.sian.com"
response.write "<li>"&server.htmlencode(formatOut(tmp,stmp,utmp))
response.write "<li>"&server.htmlencode(tmp)
hbhbhbhbhb1021 2007-02-28
  • 打赏
  • 举报
回复
我说的是JAVASCRIPT里可以,虽然可以,但是实质上也是一次一次替换,只是遍历一次
VBSCRIPT就不是很清楚了,感觉你的程序应该差不多了
yongfa365 2007-02-28
  • 打赏
  • 举报
回复
好像快可以了。随机的那个和正则一起实现,好像不成

Function RegExpTest(strng,patrn,patrn2)
Dim regEx, Match, Matches ' 建立变量。
Set regEx = New RegExp ' 建立正则表达式。
regEx.IgnoreCase = True ' 设置是否区分字符大小写。
regEx.Global = True ' 设置全局可用性。
patrn=split(patrn,"|")

For Each p in patrn
regEx.Pattern = p ' 设置模式。
'Set Matches = regEx.Execute(strng) ' 执行搜索。
'For Each Match in Matches ' 遍历匹配集合。
strng = regEx.Replace(strng, p & arrArt(patrn2) & chr(10))
'Next

RegExpTest = strng

Next
End Function

Function arrArt(patrn2)
arrArti=split(patrn2,"|")
Randomize
arrArt=arrArti(CInt(Ubound(arrArti)*Rnd))
End Function

MsgBox(RegExpTest( "111<br/>222</p>333<br/>444</p>555<Br>666</P>111<br/>222</p>333<br/>444</p>555<Br>666</P>","<br/>|</p>|<br>","www.baidu.com|www.yongfa365.com|www.sian.com"))
yongfa365 2007-02-28
  • 打赏
  • 举报
回复
按hbhbhb

还是不成,应该是正则有问题

Function RegExpTest(strng,patrn,patrn2)
Dim regEx, Match, Matches ' 建立变量。
Set regEx = New RegExp ' 建立正则表达式。
regEx.IgnoreCase = True ' 设置是否区分字符大小写。
regEx.Global = True ' 设置全局可用性。
patrn=split(patrn,"|")
For Each p in patrn
regEx.Pattern = p ' 设置模式。
Set Matches = regEx.Execute(strng) ' 执行搜索。
For Each Match in Matches ' 遍历匹配集合。
strng = regEx.Replace(strng, Match.Value & arrArt(patrn2) & chr(10)&chr(10)&chr(10))
Next
RegExpTest = strng
Next
End Function

Function arrArt(patrn2)
arrArti=split(patrn2,"|")
Randomize
arrArt=arrArti(CInt(Ubound(arrArti)*Rnd))
End Function

MsgBox(RegExpTest( "asdfas1111111df<br/>asdddd222222dddd</p>asdf3333asdf<br/>asddd444444444ddddd</p>asdfas555555df<Br>asdddddd66dd</P>","<br/>|</p>|<br>","www.baidu.com|www.yongfa365.com|www.sian.com"))
hbhbhbhbhb1021 2007-02-28
  • 打赏
  • 举报
回复
JS可以替换replace函数的第二个参数可以是个函数,那样就可以了按自己的功能替换了,VBscript没试过
yongfa365 2007-02-28
  • 打赏
  • 举报
回复
哦,现在我就是这样弄的,不过感觉,太不专业了。
jspadmin 2007-02-28
  • 打赏
  • 举报
回复
那你就在replace加个参数,让每次替换的内容随机替换
也就是说,第一个replace,你替换成a,第2个replace,替换成b
关于replace的可选参数,自己搜索研究下
实在不行,就用instr先判断<p>是第几个,再来定位随机替换
yongfa365 2007-02-28
  • 打赏
  • 举报
回复
这样替换后有一个问题:所有段落后加的内容都一样,而初忠是:加的内容是随机的一些内容。
jspadmin 2007-02-28
  • 打赏
  • 举报
回复
用vbs相对简单些,直接replace("<p>","<p>你要加的内容")

4,008

社区成员

发帖
与我相关
我的任务
社区描述
它是一种微软环境下的轻量级的解释型语言,它使用COM组件、WMI、WSH、ADSI访问系统中的元素,对系统进行管理。
社区管理员
  • vbScript社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧