Search Keyword Highlight

Results 1 to 2 of 2

Thread: Search Keyword Highlight

  1. #1
    Join Date
    Dec 1969

    Default Search Keyword Highlight

    I am trying to create a highlight keyword functionality for a website search. What I would like to do is highlight using a stylesheeted &#060;span&#062;&#060;/span&#062; all occurances of the keyword the user types into the search after being presented with the search results. The source string that I am applying the reg exp test to is HTML, so the challenge is that I don&#039;t want the keyword highlighted if it is part of a URL (e.g. &#060;a href=.....&#062;Keyword&#060;/a&#062;), since it will break that HTML.<BR><BR>So what I am looking for is a way to match & replace instances of a users search keywords within the plain text of an HTML page, whilst ignoring the exact same text if it appears within an HTML tag.<BR><BR>I am after a solution for classic ASP. <BR><BR>Thanks

  2. #2
    Join Date
    Dec 1969

    Default RE: Search Keyword Highlight

    You might be able to use look aheads to make one beautiful regular expression that would do the whole job for you. I&#039;ll suggest a more brute force approach here. Look at the following regular expression I found on 4guys for stripping HTML tags from a string:<BR><BR>Dim regEx, Match, Matches &#039; Create variable.<BR>Set regEx = New RegExp &#039; Create a regular expression.<BR>&#039;regEx.Pattern = "&#060;(.&#124
    )+?&#062;" &#039; Set pattern.<BR>regEx.IgnoreCase = True &#039; Set case insensitivity.<BR>regEx.Global = True &#039; Set global applicability.<BR><BR>Set Matches = regEx.Execute("&#060;a href=dog&#062;dog&#060;/a&#062;") &#039; Execute search.<BR>For Each Match in Matches &#039; Iterate Matches collection.<BR> RetStr = RetStr & "Match found at position "<BR> RetStr = RetStr & Match.FirstIndex & ". Match Value is &#039;"<BR> RetStr = RetStr & Match.Value & "&#039;." & vbCRLF<BR>Next<BR><BR>alert( RetStr)<BR><BR>The plan would be to loop through your mathces collection to find the parts of your string that aren&#039;t HTML (You would be making heavy use of Mid, Match.FirstIndex, and Len(Match.Value)) Now since you now know which parts of the string are HTML and which are text you can simple use a replace() on the search word.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts