Skip to main content
July 19, 2006
Question

Regular Expression / replace Function Help

  • July 19, 2006
  • 1 reply
  • 345 views
The problem:

cfset myString = "i am a big boy"
cfset outputString = replace("i am a big boy","i","you","all")

Wrong Output:
you am a byoug boy

Intended Output
you am a big boy

How do I achive that output.
    This topic has been closed for replies.

    1 reply

    Inspiring
    July 19, 2006
    you have the right function but wrong attribute. If you give "All" as scope, it will replace every single "i" within your sentence with "you". So just put

    replace("i am a big boy","i","you")
    July 20, 2006
    Thanks for the input but its a little more complex that you think. Please see ethe exmaple below:

    I need the "all" attribute to replace all the stand alone "i" in the sentence.

    Example:
    cfset myString = "i am a big boy i am a big boy i am a big boy i am a big boy"
    cfset outputString = replace(myString,"i","you","all")

    How do I achieve that. Thanks
    Inspiring
    July 20, 2006
    Your first example had only one sentence. That's why I gave that answer.

    Anyway for your real question, you need to use regular expressions

    #rereplace(myString,"i\s","you ","all")# - will give u

    you am a big boy you am a big boy you am a big boy you am a big boy.

    \s looks for a whitespace character after the letter i. So that way it will not change the letter i in big