Skip to main content
Known Participant
May 25, 2007
Question

UDF Help

  • May 25, 2007
  • 3 replies
  • 386 views
I am trying to write a UDF for replacing some characters from my string. I am using replace list but somehow its just replacing only first character but not the others in the list
Here is what I am trying to do

<cfscript>
function Rep(theString){
var remove_char_list = "&, |, \, >, <, :";
var replace_char_list = "and, ' ''';
return ReplaceList(theString, remove_char_list, replace_char_list);
}
</cfscript>
basically & to be removed and (XML), and all other with empty strings, with this it just replace all occurences of & whereas others are not being replaced

Any comments??
    This topic has been closed for replies.

    3 replies

    Inspiring
    May 25, 2007
    What is your actual requirement here? ie: what is the issue you're trying
    to resolve in writing this UDF?

    --
    Adam
    Inspiring
    May 25, 2007
    Don't you need to have an equal number of elements in your remove and replace lists?

    e.g.:
    <cfscript>
    function Rep(theString){
    var remove_char_list = "&, |, \, >, <, :";
    var replace_char_list = "and, , ,&gt;, &lt;, '';
    return ReplaceList(theString, remove_char_list, replace_char_list);
    }
    </cfscript>
    Inspiring
    May 25, 2007
    Now that I've read your reply, it seems like a better match would be to do 2 replaces - 1st replace swaps out the ampersand (&), 2nd replace uses regular expressions to remove everything else

    <cfset sMyString = Replace(sMyString, "&", "and", "all")>
    <cfset sMyString = ReReplace(sMyString , "[<>'""/]", "", "all")>
    RonFusionAuthor
    Known Participant
    May 25, 2007
    My main intent is to get rid of all the replaces that I was using before and just ReplaceList because most of the characters are to be replaced with empty string and only & with and