Convert sanetize function from VBScript to JavaScript

conrad2010
conrad2010 used Ask the Experts™
on
This is an ASP function... how would this work in JavaScript keeping the same syntax as ASP?


function sanitize(strText)

      strText = REPLACE(strText,"'","''")
      strText = REPLACE(strText,"~","")
      strText = REPLACE(strText,"@","")
      strText = REPLACE(strText,"#","")
      strText = REPLACE(strText,"$","")
      strText = REPLACE(strText,"%","")
      strText = REPLACE(strText,"^","")
      strText = REPLACE(strText,"&","")
      strText = REPLACE(strText,"[","")
      strText = REPLACE(strText,"]","")
      strText = REPLACE(strText,"{","")
      strText = REPLACE(strText,"}","")
      strText = REPLACE(strText,"|","")
      strText = REPLACE(strText,"\","")
      strText = REPLACE(strText,"/","")
      strText = REPLACE(strText,"<","")
      strText = REPLACE(strText,">","")
      strText = REPLACE(strText,"EXEC","")
      
      sanitize = strText
      
end function
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2011
Commented:
something like:

stext = stext.replace('old', 'new');


function sanitize(strText)
{
      strText = strText.replace(strText, '');
      strText =  strText.replace(strText,'~');
      strText =  strText.replace(strText,'@');
      strText =  strText.replace(strText,'#');
      strText =  strText.replace(strText,'$');
      strText =  strText.replace(strText,'%');
      strText =  strText.replace(strText,'^');
      strText =  strText.replace(strText,'&');
      strText =  strText.replace(strText,'[');
      strText =  strText.replace(strText,']');
      strText =  strText.replace(strText,'{');
      strText =  strText.replace(strText,'}');
      strText =  strText.replace(strText,'|');
      strText =  strText.replace(strText,'\');
      strText =  strText.replace(strText,'/');
      strText =  strText.replace(strText,'<');
      strText =  strText.replace(strText,'>');
      strText =  strText.replace(strText,'EXEC');
      }
    
end function 

Open in new window


You can also use double quotes in places of single quotes.

One last example:

myname=myname.replace(/s/gi,'~');

Replaces all s with '~'!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial