Skip to main content
Participating Frequently
April 12, 2007
Question

Replacing quotes using ReplaceList

  • April 12, 2007
  • 4 replies
  • 1389 views
I'm trying to search a string to replace single quotes(') and double quotes(") with \' and " respectively using the ReplaceList function. However, the syntax for the function uses quotes, so the quotes are being interpreted as delimiting my syntax rather than as the object of my search/replace.
Again, I want to replace ' with '\
and " with "
What I tried was ReplaceList(MyVariable, """, '", "", \'", "ALL")#')

I can overcome this with two separate replace functions, such as:
#Replace(MyVariable, """", """, "ALL")# and
#Replace(MyVariable, "'", "\'", "ALL")#

However, I feel like I'm cheating because I couldn't figure out how to do it in one command using ReplaceList. Programers, I'm sure you understand! 🙂 Can anyone tip me off as to how to make it work with ReplaceList?

    This topic has been closed for replies.

    4 replies

    Inspiring
    April 12, 2007
    In this case, I would just use a CF RegExp function. Something like:
    ReReplaceNoCase(myDBFieldData,"'","\'","ALL")

    That should strtip out all single quotes and replace it with the escaped single quote: \'
    For the double quotes, I would use the same function:
    ReReplaceNoCase(myDBFieldData,"""","&quote;","ALL")

    This should take out the double quotes and replace with the HTML entity. I believe that in this function you need to let CF know that a double quote is coming by escaping it in the function with "". Similar to using the ## when referencing a Hex color inside a cfoutput block.
    Inspiring
    April 12, 2007
    I would have done it with two replace functions, just as you did.
    Inspiring
    April 12, 2007
    Just out of curiousity, why do you want to replace the quotes? Depending on the reason, it might not be necessary.
    bbenjaminAuthor
    Participating Frequently
    April 12, 2007
    I'm pulling data from a db which is being used by a Javascript called overlib, which displays a box of text when the mouse is over the link. The script's docs mandate that quotes and single quotes be replaced. If you look at http://www.bosrup.com/web/overlib/?Documentation and then #4 on that page, it explains it better.
    Inspiring
    April 12, 2007
    I'm curious, why ReplaceList and not ReReplace? Is the value of #MyVariable# a true list or a regular string?

    I was going to run a test on the function but wanted to check on what the content (or sample contents) of #MyVariable# is?
    bbenjaminAuthor
    Participating Frequently
    April 12, 2007
    The variable is actually just a paragraph of text being pulled from a field in a db. I have to strip out the quotes because the data in the field is being using in a Javascript which mandates that. Does that help?