SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2004
    Location
    UK
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Replacing words regular expression

    Hi

    I have a search results page that lists a series of records with clickable links to view the records. When you click on the link to the record, the record id, the search terms used, and the search type (exactphrase or anywords) is sent through the query string.

    On the record page I have been trying to find a regular expression that would find and replace the search terms with < strong >< /strong > to highlight them. I am struggling.

    Does any use a similar regular expression. One that, if flagged, will replace an exact string (phrase) or loop through an array replacing any of the words in the string?

    This has me absolutely stumped!

    Many thanks for any help.

    Cheers

    Ray

  2. #2
    Original Gangster silver trophy Thing's Avatar
    Join Date
    Oct 2000
    Location
    Philadelphia, PA
    Posts
    4,708
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Why not use a REPLACE function?

    Code:
    <%
    Function RepTerms(txtBody, txtTerm)
      variable = REPLACE(txtBody, txtTerm, "<strong>" & txtTerm & "</strong>")
      RepTerms = variable
    End Function
    %>
     
     
    <%
    'call the function
    txtBody = "This is the sentence that I want to search for the keyword in"
    txtTerm = "sentence"
    response.write RepTerms(txtBody, txtTerm)
    %>

  3. #3
    SitePoint Enthusiast
    Join Date
    Nov 2004
    Location
    UK
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for that. I had just got Reg Ex working. This works fine, but it also replaces words within html tags (for example, alt tags) which screws them. Is there a way round it?

    If strHighlightType = "anywords" Then

    Function highlightQuery(strText)

    SearchWordsCount = Ubound(arrHighlightWords)
    For c=0 to SearchWordsCount

    strText = " " & strText
    strText = eregReplace(strText, arrHighlightWords(c), "<span class=""highlight"">" & arrHighlightWords(c) & "</span>")
    strText = right(strText, len(strText)-1)

    Next

    highlightQuery = strText
    End Function

    End If


    Function eregReplace(strOriginalString, strPattern, strReplacement)
    ' Function replaces pattern with replacement
    dim objRegExp : set objRegExp = new RegExp
    objRegExp.Pattern = strPattern
    objRegExp.IgnoreCase = True
    objRegExp.Global = True
    eregReplace = objRegExp.Replace(strOriginalString, strReplacement)
    set objRegExp = nothing
    End Function


    Many, many thanks for any help!!

    Cheers

    Del


Bookmarks

Posting Permissions

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