Remove carriage returns from a string

Quite often I need to remove carriage returns and usually I use the following code:


However I have found this doesn’t always remove the carriage returns.

How did I fix it? I used the following code:


This works everytime, its very handy for writing html with javascript

for example:

<cfsavecontent variable=”thecode”>
<cfinclude template=”#dynamicCFMfile#”>

newDefaultCode = Replace(thecode ,””””,”\”””,”ALL”);
newDefaultCode = REReplace(newDefaultCode ,”#chr(13)#|#chr(9)#|\n|\r”,””,”ALL”);
<cfoutput>theHTML = “#newDefaultCode#”;</cfoutput>


This allows me to dynamically write a coldfusion template to the page via javascript.


April 10, 2008. Tags: , , , , . coldfusion, Javascript.


  1. Steven Benjamin replied:

    Thank you for posting this.

  2. msingh replied:

    Amazing…you just saved me a bottle of scotch…I owe you one.

  3. damiththa replied:

    this is a the best, finally found the solution for bugging me for days, literally days.
    Thank you, works like a charm.

  4. Frank Tudor replied:

    Thanks for this post!!! Dynamic string creation over here is fails…Your snippet fixed the problem…

  5. Brant Kelsey replied:

    I had better luck with:


    …like the example you gave below.

    #chr(9)# was missing from the upper snippet.

    Regardless, this works like a charm! THANK YOU!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Trackback URI

%d bloggers like this: