突出显示经典ASP中的关键字

时间:2013-03-08 08:41:25

标签: asp-classic highlight

我有这句话,“男人出去了。”

我还有4个搜索标准我想得到高分(忽略括号),[go |“an WeNT o”| a | t] [span id =“something”] [/ span]。

我尝试了很多东西,但我无法弄清楚如何在经典的ASP中做到这一点!?如果我在文本中插入某个地方,它也会搜索HTML代码以寻找SPAN,这很糟糕,或者因为它与HTML代码混淆而无法找到文本。我也尝试插入原始文本中的所有位置,甚至使用一些我不理解的魔术正则表达式,但我无法使其工作: - /

搜索事物除以|并且可以是1到20个要搜索的东西。

任何人都可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

我找到并调整了一些代码,它对我来说很有效:

Function highlightStr (haystack, needles)

' Taken (and tweaked) from these two sites:
' http://forums.aspfree.com/asp-development-5/asp-highlight-keywords-295641.html
' http://www.eggheadcafe.com/forumarchives/scriptingVisualBasicscript/Jul2005/post23377133.asp
'    
' INPUT: haystack = search in this string
' INPUT: needles = searches divided by |... example: this|"is a"|search
' OUTPUT: HTML formatted highlighted string
'
If Len(haystack) > 0 Then

  ' Delete the first and the last array separator "|" (if any)
  If Left(needles,1) = "|" Then needles = Right(needles,Len(needles)-1)
  If Right(needles,1) = "|" Then needles = Mid(needles,1,Len(needles)-1)

  ' Delete a multiple seperator (if any)
  needles = Replace(needles,"||","|")

  ' Delete the exact-search chars (if any)
  needles = Replace(needles,"""","")

  ' Escape all special regular expression chars
  needles = Replace(needles,"(","\(")
  needles = Replace(needles,")","\)")
  needles = Replace(needles,".","\.")

  If Len(needles) > 0 Then
    haystack = " " & haystack & " "
    Set re = New RegExp
    re.Pattern = "(" & needles & ")"
    re.IgnoreCase = True 
    re.Global = True 
    highlightStr = re.Replace(haystack,"<span style='background-color:khaki;'>$&</span>")
  Else
    highlightStr = haystack
  End If

Else
  highlightStr = haystack
End If

End Function