468,119 Members | 2,072 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,119 developers. It's quick & easy.

Highlighting searching keyword

hiiiiiii
I have a problem in highlighting searching keyword.Actually im using this function for searching
Expand|Select|Wrap|Line Numbers
  1. Public Function HighLight(ByVal Keyword As String, ByVal ContentFor As String)
  2.  
  3.     Dim objHighLight As New highlight(Keyword, "<span class='searchKeyword'>{keyword}</span>")
  4.  
  5.     ContentFor = objHighLight.process(ContentFor, False, False)
  6.     Return ContentFor
  7.     'Dim RegExp As Regex = New Regex(Keyword.Replace(" ", "|").Trim(), RegexOptions.IgnoreCase)
  8.     '' Highlight keywords by calling the MatchEvaluator delegate each time a keyword is found.
  9.     'HighLight = RegExp.Replace(ContentFor, New MatchEvaluator(AddressOf ReplaceKeyWords))
  10.     '' Set the Regex to nothing.
  11.     'RegExp = Nothing
  12. End Function
  13.  
After that i had made a class Highlight.vb in which i defined process function and all procedure of highlighting.
HIGHLIGHT.VB
/start/
Expand|Select|Wrap|Line Numbers
  1.  
  2. Imports Microsoft.VisualBasic
  3. Imports System.Text.RegularExpressions
  4. Imports Common
  5. Public Class highlight
  6. Dim keyword As String
  7. Dim replacement As String
  8. Dim highLightBadTags() As String = New String() {"A", "IMG", "SPAN"}
  9. Dim objCommon As New Common
  10. 'constructor
  11. Sub New(ByVal keyword As String, ByVal replacement As String)
  12.     Me.keyword = keyword
  13.     Me.replacement = replacement
  14. End Sub
  15. Private Function highLighterHTML(ByVal matches As Match) As String
  16.     Dim proceed As String
  17.     'check for bad tags and keyword
  18.     'HttpContext.Current.Response.Write(matches.Groups(0).ToString & " --- " & matches.Groups(1).ToString & " --- " & matches.Groups(2).ToString & " --- " & matches.Groups(3).ToString & " --- " & Me.objCommon.FindInArray(matches.Groups(2).ToString.ToUpper, Me.highLightBadTags) & "<br>")
  19.     'HttpContext.Current.Response.Write(matches.Groups(2).ToString & " --- " & matches.Groups(3).ToString & " --- " & Me.objCommon.FindInArray(matches.Groups(2).ToString.ToUpper, Me.highLightBadTags) & "<br>")
  20.     If Not Me.objCommon.FindInArray(matches.Groups(2).ToString.ToUpper, Me.highLightBadTags) Then
  21.     'HttpContext.Current.Response.Write(matches.Groups(2).ToString.ToUpper & " --- " & Me.objCommon.FindInArray(matches.Groups(2).ToString.ToUpper, Me.highLightBadTags) & "<br>")
  22.         If Regex.IsMatch(matches.Groups(0).ToString, "\b(" & Me.keyword & ")\b", RegexOptions.IgnorePatternWhitespace Or RegexOptions.Singleline Or RegexOptions.IgnoreCase) Then
  23.             'put template {keyword} replacement
  24.             proceed = Regex.Replace(matches.Groups(0).ToString, "\b(" & Me.keyword & ")\b", Replace(Me.replacement, "{keyword}", matches.Groups(3).ToString), RegexOptions.IgnorePatternWhitespace Or RegexOptions.Singleline Or RegexOptions.IgnoreCase)
  25.         End If
  26.     Else
  27.         proceed = matches.Groups(0).ToString
  28.     End If
  29.     Return proceed
  30. End Function
  31. Private Function highLighterText(ByVal matches As Match) As String
  32.     Dim proceed As String
  33.     'check for bad tags and keyword
  34.     If Not Me.objCommon.FindInArray(matches.Groups(1).ToString.ToUpper, Me.highLightBadTags) Then
  35.         If Regex.IsMatch(matches.Groups(0).ToString, "(" & Me.keyword & ")", RegexOptions.IgnorePatternWhitespace Or RegexOptions.Singleline Or RegexOptions.IgnoreCase) Then
  36.             'put template {keyword} replacement
  37.             proceed = Regex.Replace(matches.Groups(0).ToString, "(" & Me.keyword & ")", Replace(Me.replacement, "{keyword}", matches.Groups(0).ToString), RegexOptions.IgnorePatternWhitespace Or RegexOptions.Singleline Or RegexOptions.IgnoreCase)
  38.         Else
  39.             proceed = matches.Groups(0).ToString
  40.         End If
  41.     Else
  42.         proceed = matches.Groups(0).ToString
  43.     End If
  44.  
  45.     Return proceed
  46. End Function
  47. 'main api
  48. Public Function process(ByVal text As String, ByVal keyword As Boolean, ByVal replacement As Boolean)
  49.  
  50.     'if there are specific keyword/replacement given
  51.     If keyword Then
  52.         Me.keyword = keyword
  53.     End If
  54.     If replacement Then
  55.         Me.replacement = replacement
  56.     End If
  57.     If Me.keyword IsNot Nothing AndAlso Me.replacement IsNot Nothing Then
  58.         If Regex.IsMatch(text, "(<([A-Za-z]+)[^>]*[\>]*)*(" & Me.keyword & ")\b(.*?)(<\/\\2>)*#si", RegexOptions.IgnorePatternWhitespace Or RegexOptions.Singleline Or RegexOptions.IgnoreCase) Then
  59.             Return Regex.Replace(text, "(<([A-Za-z]+)[^>]*[\>]*)*(" & Me.keyword & ")\b(.*?)(<\/\\2>)*#si", New MatchEvaluator(AddressOf highLighterHTML), RegexOptions.IgnorePatternWhitespace Or RegexOptions.Singleline Or RegexOptions.IgnoreCase)
  60.     Else
  61.         Return Regex.Replace(text, "(" & Me.keyword & ")", New MatchEvaluator(AddressOf highLighterText), RegexOptions.IgnorePatternWhitespace Or RegexOptions.Singleline Or RegexOptions.IgnoreCase)
  62.     End If
  63.     'HttpContext.Current.Response.Write(Regex.Replace(text, "(<([A-Za-z]+)[^>]*[\>]*)*(" & Me.keyword & ")\b(.*?)(<\/\\2>)*si", New MatchEvaluator(AddressOf highLighter), RegexOptions.IgnorePatternWhitespace Or RegexOptions.Singleline Or RegexOptions.IgnoreCase))
  64.     'Return Regex.Replace(text, "\b(<([A-Za-z]+)[^>]*[\>]*)*(" & Me.keyword & ")\b(.*?)(<\/\\2>)*si", New MatchEvaluator(AddressOf highLighter), RegexOptions.IgnorePatternWhitespace Or RegexOptions.Singleline Or RegexOptions.IgnoreCase)
  65.     Else
  66.         Return text
  67.     End If
  68. End Function
  69.  
  70. End Class
Now what the problem i got is that i have a string in the text to be search
Expand|Select|Wrap|Line Numbers
  1. <p><img height="161" alt="Polly Gotseva" width="168" align="left" src="/userfiles/polly.jpg" /><b>Polly Gotseva, Managing Director</b></p>
when i search polly keyword it will not show complete image and take string like this
Expand|Select|Wrap|Line Numbers
  1. <p><img height="161" alt="<span class='searchKeyword'>Polly</span> Gotseva" width="168" align="left" src="/userfiles/<span class='searchKeyword'>Polly</span>.jpg" /><b><span class='searchKeyword'>Polly</span> Gotseva, Managing Director</b></p>
now because of image path changed it doesnt show image.That is my problem


if anyone understands my problem please reply soon.Its very urgent
Feb 2 '09 #1
1 2548
Frinavale
9,735 Expert Mod 8TB
You have to make sure that the word you're searching for does not exist as an attribute of a HTML tag before surrounding the word in a <span>.

You have to write a regular expression that checks that makes sure that the word is not replaced if the word for exists after a "<" and before a ">".
Feb 2 '09 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

9 posts views Thread by Jay Davis | last post: by
reply views Thread by Chris Chandler | last post: by
3 posts views Thread by Antoine Junod | last post: by
4 posts views Thread by Bob hotmail.com> | last post: by
7 posts views Thread by Leif902 | last post: by
4 posts views Thread by CreativeMind | last post: by
14 posts views Thread by didacticone | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.