I have a problem in highlighting searching keyword.Actually im using this function for searching
Expand|Select|Wrap|Line Numbers
- Public Function HighLight(ByVal Keyword As String, ByVal ContentFor As String)
- Dim objHighLight As New highlight(Keyword, "<span class='searchKeyword'>{keyword}</span>")
- ContentFor = objHighLight.process(ContentFor, False, False)
- Return ContentFor
- 'Dim RegExp As Regex = New Regex(Keyword.Replace(" ", "|").Trim(), RegexOptions.IgnoreCase)
- '' Highlight keywords by calling the MatchEvaluator delegate each time a keyword is found.
- 'HighLight = RegExp.Replace(ContentFor, New MatchEvaluator(AddressOf ReplaceKeyWords))
- '' Set the Regex to nothing.
- 'RegExp = Nothing
- End Function
HIGHLIGHT.VB
/start/
Expand|Select|Wrap|Line Numbers
- Imports Microsoft.VisualBasic
- Imports System.Text.RegularExpressions
- Imports Common
- Public Class highlight
- Dim keyword As String
- Dim replacement As String
- Dim highLightBadTags() As String = New String() {"A", "IMG", "SPAN"}
- Dim objCommon As New Common
- 'constructor
- Sub New(ByVal keyword As String, ByVal replacement As String)
- Me.keyword = keyword
- Me.replacement = replacement
- End Sub
- Private Function highLighterHTML(ByVal matches As Match) As String
- Dim proceed As String
- 'check for bad tags and keyword
- '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>")
- 'HttpContext.Current.Response.Write(matches.Groups(2).ToString & " --- " & matches.Groups(3).ToString & " --- " & Me.objCommon.FindInArray(matches.Groups(2).ToString.ToUpper, Me.highLightBadTags) & "<br>")
- If Not Me.objCommon.FindInArray(matches.Groups(2).ToString.ToUpper, Me.highLightBadTags) Then
- 'HttpContext.Current.Response.Write(matches.Groups(2).ToString.ToUpper & " --- " & Me.objCommon.FindInArray(matches.Groups(2).ToString.ToUpper, Me.highLightBadTags) & "<br>")
- If Regex.IsMatch(matches.Groups(0).ToString, "\b(" & Me.keyword & ")\b", RegexOptions.IgnorePatternWhitespace Or RegexOptions.Singleline Or RegexOptions.IgnoreCase) Then
- 'put template {keyword} replacement
- 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)
- End If
- Else
- proceed = matches.Groups(0).ToString
- End If
- Return proceed
- End Function
- Private Function highLighterText(ByVal matches As Match) As String
- Dim proceed As String
- 'check for bad tags and keyword
- If Not Me.objCommon.FindInArray(matches.Groups(1).ToString.ToUpper, Me.highLightBadTags) Then
- If Regex.IsMatch(matches.Groups(0).ToString, "(" & Me.keyword & ")", RegexOptions.IgnorePatternWhitespace Or RegexOptions.Singleline Or RegexOptions.IgnoreCase) Then
- 'put template {keyword} replacement
- 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)
- Else
- proceed = matches.Groups(0).ToString
- End If
- Else
- proceed = matches.Groups(0).ToString
- End If
- Return proceed
- End Function
- 'main api
- Public Function process(ByVal text As String, ByVal keyword As Boolean, ByVal replacement As Boolean)
- 'if there are specific keyword/replacement given
- If keyword Then
- Me.keyword = keyword
- End If
- If replacement Then
- Me.replacement = replacement
- End If
- If Me.keyword IsNot Nothing AndAlso Me.replacement IsNot Nothing Then
- If Regex.IsMatch(text, "(<([A-Za-z]+)[^>]*[\>]*)*(" & Me.keyword & ")\b(.*?)(<\/\\2>)*#si", RegexOptions.IgnorePatternWhitespace Or RegexOptions.Singleline Or RegexOptions.IgnoreCase) Then
- Return Regex.Replace(text, "(<([A-Za-z]+)[^>]*[\>]*)*(" & Me.keyword & ")\b(.*?)(<\/\\2>)*#si", New MatchEvaluator(AddressOf highLighterHTML), RegexOptions.IgnorePatternWhitespace Or RegexOptions.Singleline Or RegexOptions.IgnoreCase)
- Else
- Return Regex.Replace(text, "(" & Me.keyword & ")", New MatchEvaluator(AddressOf highLighterText), RegexOptions.IgnorePatternWhitespace Or RegexOptions.Singleline Or RegexOptions.IgnoreCase)
- End If
- '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))
- '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)
- Else
- Return text
- End If
- End Function
- End Class
Expand|Select|Wrap|Line Numbers
- <p><img height="161" alt="Polly Gotseva" width="168" align="left" src="/userfiles/polly.jpg" /><b>Polly Gotseva, Managing Director</b></p>
Expand|Select|Wrap|Line Numbers
- <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>
if anyone understands my problem please reply soon.Its very urgent