使用正则表达式实现模式图片新闻(加强).ASP

网络整理 - 07-26
客户要求保存原有的img已在的属性,郁闷,继续改

代码如下:


function shownew(content,ntype)

shownew = ""

if(ntype = 1) then

shownew = shownew &" <TABLE width=100% >"
shownew = shownew &"<TR>"
shownew = shownew &" <TD width=700 align=center valign=top>"&ShowPic(content)&"</TD>"
shownew = shownew &"</TR>"
shownew = shownew &"<TR>"
shownew = shownew &" <TD valign=top>"&OnlyWord(content)&"</TD>"
shownew = shownew &"</TR>"
shownew = shownew &"</TABLE>"


elseif (ntype = 2) then


shownew = shownew &" <TABLE>"

shownew = shownew &"<TR>"
shownew = shownew &" <TD valign=top>"&OnlyWord(content)&"</TD>"
shownew = shownew &"</TR>"
shownew = shownew &"<TR>"
shownew = shownew &" <TD align=center width=700 valign=top>"&ShowPic(content)&"</TD>"
shownew = shownew &"</TR>"
shownew = shownew &"</TABLE>"

elseif (ntype = 3) then


shownew = shownew &"<TABLE>"

shownew = shownew &"<TR>"
shownew = shownew &"<TD width=100 align=center valign=top>"&ShowPic(content)&"</TD>"

shownew = shownew &"<TD width=80% valign=top>"&OnlyWord(content)&"</TD>"


shownew = shownew &"</TR>"
shownew = shownew &"</TABLE>"

elseif (ntype = 4) then

shownew = shownew &"<TABLE>"

shownew = shownew &"<TR>"

shownew = shownew &"<TD width=80% valign=top>"&OnlyWord(content)&"</TD>"
shownew = shownew &"<TD width=100 valign=top align=center>"&ShowPic(content)&"</TD>"

shownew = shownew &"</TR>"
shownew = shownew &"</TABLE>"
else
shownew = shownew &content
end if

end function

'显示提取的图片
function ShowPic(strng)

set regEx2 = new RegExp

regEx2.Pattern = "(src=)('|"&CHR(34)&"| )?(.[^'| |"&CHR(34)&"]*)(\.)(jpg|gif|png|bmp|jpeg)('|"&CHR(34)&"| |>)?" '设置模式。
regEx2.IgnoreCase = true '设置是否区分字符大小写。
regEx2.Global = True '设置全局可用性。

ShowPic = ""
ImageUrl= RegExpExecute(strng)

ImageUrls = Split(ImageUrl,"{|LDIV|}")


for i = LBound(ImageUrls) to (UBound(ImageUrls) - 1)


Set Matches2 = regEx2.Execute(ImageUrls(i)) '执行搜索。


For Each Match2 in Matches2 '遍历匹配集合。

'全Img标签
newpic = LCase(ImageUrls(i))

'如果没有自定义BORDER增加BOrder=0,其它原属性保留

if(instr(newpic,"border")=false) then newpic = left(newpic,len(newpic)-1) + " >"
if(instr(newpic,"width")=false) then newpic = left(newpic,len(newpic)-1) + " >"

newpic = "<a href='"&Match2.SubMatches(2)&Match2.SubMatches(3)&Match2.SubMatches(4)&"' >"& newpic & "</a>"

ShowPic = ShowPic & newpic
Next


next

Set regEx2 = nothing
end function


'从内容中提取图片
Function RegExpExecute(strng)
Dim regEx, Match, Matches '建立变量。
Set regEx = New RegExp '建立正则表达式。

regEx.Pattern = "(<)(.[^<]*)(src=)('|"&CHR(34)&"| )?(.[^'|\s|"&CHR(34)&"]*)(\.)(jpg|gif|png|bmp|jpeg)('|"&CHR(34)&"|\s|>)(.[^>]*)(>)" '设置模式。


regEx.IgnoreCase = true '设置是否区分字符大小写。
regEx.Global = True '设置全局可用性。
Set Matches = regEx.Execute(strng) '执行搜索。
For Each Match in Matches '遍历匹配集合。
values=values&Match.Value&"{|LDIV|}"
Next
RegExpExecute = values
End Function

'删除内容中与图片有关的代码
function OnlyWord(strng)
Set re=new RegExp
re.IgnoreCase =True
re.Global=True

re.Pattern = "(<)(.[^<]*)(src=)('|"&CHR(34)&"| )?(.[^'|\s|"&CHR(34)&"]*)(\.)(jpg|gif|png|bmp|jpeg)('|"&CHR(34)&"|\s|>)(.[^>]*)(>)" '设置模式。
OnlyWord=re.Replace(strng,"")
Set re= nothing

end function