Skip to main content
Known Participant
March 4, 2009
Answered

replace exact string only

  • March 4, 2009
  • 1 reply
  • 444 views
I'm outputting text content to my page by querying our database using a simple method, which works fine.
quote:


<cfoutput query="getPageContent" startrow="#vPageContent#" maxrows="1">#vContent#</cfoutput>


I need to replace certain words to make them bold, for example...
quote:


<cfset request.vContent = replace(request.vContent, "phrase", "<B>phrase</B>, "ALL")>


This works, but there is a slight issue, because it will also replace occurances of the word 'phrase' even if it is within another word...

for example, co phrasetastic

So i need to replace only the exact phrase when used on it's own.

My problem appears quite simple, but I have not found a way to do this...?
    This topic has been closed for replies.
    Correct answer Newsgroup_User
    Sam_Ham wrote:
    > <cfset request.vContent = replace(request.vContent, "phrase", " phrase,
    > "ALL")>

    note the spaces in the following:

    <cfset request.vContent=replace(request.vContent," phrase "," phrase
    ","ALL")>

    or use regex via the reReplace method with something like the "transition
    boundary" escape sequence "\b" (check the syntax, doing this from poor memory):
    reReplace(vContent,"\bphrase\b"," phrase","ALL")

    1 reply

    Newsgroup_UserCorrect answer
    Inspiring
    March 4, 2009
    Sam_Ham wrote:
    > <cfset request.vContent = replace(request.vContent, "phrase", " phrase,
    > "ALL")>

    note the spaces in the following:

    <cfset request.vContent=replace(request.vContent," phrase "," phrase
    ","ALL")>

    or use regex via the reReplace method with something like the "transition
    boundary" escape sequence "\b" (check the syntax, doing this from poor memory):
    reReplace(vContent,"\bphrase\b"," phrase","ALL")
    Sam_HamAuthor
    Known Participant
    March 4, 2009
    quote:

    Originally posted by: Newsgroup User
    Sam_Ham wrote:
    > <cfset request.vContent = replace(request.vContent, "phrase", " phrase,
    > "ALL")>

    note the spaces in the following:

    <cfset request.vContent=replace(request.vContent," phrase "," phrase
    ","ALL")>

    or use regex via the reReplace method with something like the "transition
    boundary" escape sequence "\b" (check the syntax, doing this from poor memory):
    reReplace(vContent,"\bphrase\b"," phrase","ALL")



    Thanks Paul... it works like a charm!