0

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

我也有 4 个搜索条件,我想获得高分(忽略括号),[went|"an WeNT o"|a|t] 和 [span id="something"][/span]。

我已经尝试了很多东西,但我不知道如何在经典的 ASP 中做到这一点!?如果我在文本中的某处插入一个,它也会在 HTML 代码中搜索 SPAN,这很糟糕,或者它不会找到文本,因为它已经被 HTML 代码弄乱了。我还尝试插入原始文本中的所有位置,甚至使用一些我不理解但无法正常工作的神奇正则表达式:-/

搜索的东西被划分为 | 并且可以是 1 到 20 个要搜索的东西。

谁能帮我解决如何做到这一点?

4

1 回答 1

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
于 2013-03-11T07:12:14.600 回答