New Iteration of cleanASCII

Bob Sneidar bobsneidar at iotecdigital.com
Wed Mar 22 16:29:05 EDT 2017


Hi all. 

A while back I wrote a function that would remove all characters from a string except for 0-9, a-z, and A-Z. I have updated it to also include an arguement for other allowed characters, and true or false for line breaks and or carriage returns. Note the code below. What I need is a list of reserved regex characters, like backslash (\) and whatnot. 

Once tuned up it can be added to the Master Library (I think an older version may be there already). 

function cleanASCII pString, pAllowedChars, pAllowBreaks
   put "*|\" into lReservedChars
   put pAllowedChars into tCharList
   
   repeat for each char tChar in pAllowedChars
      if tChar is in lReservedChars then replace tChar with "\" & tChar in tCharList
   end repeat
   
   if pAllowBreaks is empty then put false into pAllowBreaks
   
   put "[ 0-9A-Za-z]" into tMatchText
   put tCharList after char 2 of tMatchText
   
   if pAllowBreaks then 
      put "\r\n" after char 1 of tMatchText
   end if
   
   repeat for each character  theChar in pString
     /* if theChar is "|" then 
         put theChar after cleanString -- special case for regex reserved char
         next repeat
      end if
      */
      if matchtext(theChar, tMatchText) is true then
         put theChar after cleanString
      end if
   end repeat
   
   return cleanString
end cleanASCII

Bob S






More information about the use-livecode mailing list