Skip to main content
June 29, 2009
Answered

regex

  • June 29, 2009
  • 4 replies
  • 657 views

I would like to replace %20 in a string with a space using a regular expression in order to insert the string into a database. I have tried to use


        ReReplace("#varriable1#","%20","[[:space:]]","ALL")

But it fails to work. Is this allowed and if so is this simply incorrect syntax?

    This topic has been closed for replies.
    Correct answer

    Hi Just try ths....

    <cfset test = ReReplace("#varriable1#","%20"," ","ALL")>

    4 replies

    Inspiring
    June 30, 2009

    You might find that there are other functions which can do this sort of job more thoroughly:

    1. Use the <cfqueryparam> tag to pass the string to the query as a parameter.  In this way, it cannot become confused with SQL syntax... "SQL injection," whether accidental or intentional, becomes impossible.
    2. Use functions like HTMLEditFormat() and HTMLCodeFormat() to do substitutions of entities.  This will handle all of them.  (You might have to diddle with it... they try to be "helpful." )

    Generally speaking, I store information in a database in unencoded forrmat, encoding it as-necessary upon display.  All of the literal-values in any <cfquery> are passed in as parameters.

    Participating Frequently
    June 29, 2009

    Using a simple replace works just as well:

    Replace(variable1, "%20", " ", "ALL")

    Notes:

    - you don't need dash signs around your variable;

    - designates a space inside a regular expression not inside

    a normal string (and the second parameter of ReReplace is a normal

    string).

    Mack

    Dileep_NR
    Inspiring
    June 29, 2009

    Hi,

    Please try this

    ReReplace("#varriable1#","%20"," ","ALL")

    In your code

    ReReplace("#varriable1#","%20","[[:space:]]","ALL")

    "%20" replaced with the string "[[:space:]]"

    eg :-     <cfset varriable1="Cold%20Fusion%20Forum">

    result

    Cold[[:space:]]Fusion[[:space:]]Forum

    Correct answer
    June 29, 2009

    Hi Just try ths....

    <cfset test = ReReplace("#varriable1#","%20"," ","ALL")>

    June 29, 2009

    Its funny how it doesn't work the first time.