asp为一段文字中的网址自动加链接asp
asp为一段文字中的网址自动加链接...
<% '为文本中的网址添加链接 Function AddLinksToUrls(text) Dim regEx ' 创建正则表达式对象 Set regEx = New RegExp ' 设置正则表达式模式,用于匹配网址 regEx.Pattern = "(http|https|ftp)://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?" ' 忽略大小写 regEx.IgnoreCase = True ' 全局匹配 regEx.Global = True ' 使用替换方法将匹配到的网址替换为HTML链接 AddLinksToUrls = regEx.Replace(text, "<a href=""$&"" target=""_blank"">$&</a>") ' 释放正则表达式对象 Set regEx = Nothing End Function ' 示例文本 Dim sampleText sampleText = "这是一个包含网址的文本,比如https://www.example.com和http://www.test.com 。" ' 调用函数为文本中的网址添加链接 Dim resultText resultText = AddLinksToUrls(sampleText) ' 输出结果 Response.Write resultText %>
我要评论