Skip to main content
Inspiring
April 21, 2009
Question

special characters

  • April 21, 2009
  • 2 replies
  • 912 views

I stored all the special chars into the list and compare with the form variable if finding any special one then replace with _ but i can't make it work, any helps are really appreciated.

<cfset SpecialChars ="/,\,(,),[,],{,},@,$,##,?,!,<,>,=,:,;,'"" ">

<cfif ListFindNocase(SpecialChars, form.center_name, ',' )>
      <cfset type = REReplace(form.center_name,"SpecialChars","_'","ALL") >
    <cfelse>
      <cfset type = #form.center_name#>
    </cfif>

Thanks,

KT

This topic has been closed for replies.

2 replies

BKBK
Community Expert
Community Expert
April 25, 2009

<cfset specialChars = "/,\,(,),[,],{,},@,$,##,?,!,<,>,=,:,;,',""">
<cfset testString = replaceList("a/b\c(d)eg{h}i@j$k##l?m!n<o>p=q:r;s't""u", specialChars, "_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_")>

<cfoutput>#testString#</cfoutput>

<!--- The advantage of replaceList() is flexibility. You are free to choose which character will replace which special character, not necessarily the underscore(_). --->

ilssac
Inspiring
April 21, 2009

First of all the "SpecialChars" in your REReplace() function does not reference your variable of that name it is the literal string SpecialChars.

Secondly your If|else block is uncessary, just use the Rereplace function that will replace the desired characters if they exist otherwise it will do nothing.

You code block can be reduced to:

<cfset type = REReplace(form.center_name,"[/\{}[]()@$#?|<>=;,"" ","_']","ALL") >

Note: I made no effort to ensure that the regex characters are properly escaped.  Some of those character will need a "\" in front of them to escape them inorder to work as desired.

P.S. There are probably easier ways to come up with the class of characters to replace with an underscore, depending on what you are really trying to do.  Maybe not letters or numbers or some such.

<cfset type = REReplace(form.center_name,"[^a-zA-Z0-9]","_","ALL") >