Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


Removing all non-alphanumeric characters from a string

Removing all non-alphanumeric characters from a string

Removing all non-alphanumeric characters from a string

I need to use the string #ClientName# to generate subdirectories.  In order to avoid a variety of potential problems, I wish to strip the string of all non-alphanumeric characters.  Is there a relatively simple function for this, or should I loop through the string one character at a time?  Also, how do I simply delete a character rather than replacing it?  Finally, if I use Replace (or ReplaceList), how can I account for all possible non-alphanumeric characters?

Thanks in advance.

RE: Removing all non-alphanumeric characters from a string

the "easiest" way i found to do so was to use the regular expression in javascript ... can you use javascript ? do you mind using reg exp ? if not, then let me know and i'll try to explain

RE: Removing all non-alphanumeric characters from a string

I don't mind using javascript, although I would need some guidance.

I have tried the following with limited success:

<CFSET #attributes.centername# = ReReplace(attributes.centername,"[[:punct:][:cntrl:]]","","all")>

However, if there is whitespace in the string, the output contains it as well.  Also, I run into problems with # in the string.  Is there some way I could account for these two characters initially?

RE: Removing all non-alphanumeric characters from a string

i'm sorry i'm in the hurry here, all i can do is either ask you to wait for monday (or for some niiiiiiiiice tt member to answer) or try to read http://developer.netscape.com/docs/manuals/communicator/jsref/corea3.htm which is ns doc on regexps

but the more i think the less i'm sure that js is the solution in your case : it's easy to pass cf variable to jscript, but it's not really feasible the other way (because jscript is CLIENT side and cf SERVER side) - still, you can pass the jscript variable to any page and there, handle it with the querystring stuffs ... but as it sounds it'll end up beeing MORE COMPLICATED than your first idea of going char by char, so i would suggest ... NOT to use my solution ...

RE: Removing all non-alphanumeric characters from a string

Thanks anyway iza!

I am getting closer, although the code is getting more cumbersome as I have embedded a ReReplace in a Replace function.  The spaces are now removed from the string as well:

<cfset attributes.centername = "   Ho &~ll and~! /\{  }=-+_$%^  Har t   ">

<cfset #attributes.centername# = Replace(ReReplace(attributes.centername,"[[:punct:][:cntrl:]]","","all"), " ","","all")>

   Ho &~ll and~! /\{  }=-+_$%^  Har t   

However, I am still stuck if there happens to be a # in the string: the first nonalphanumeric character following the # errors out.  The error occurs in the first cfset statement rather than in the statement with the functions.  The only solution I can think of is to replace # with ##, but there must be a simpler way!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close