convert string into a valid file name

I always avoid struggling with file system because of its potential to be dangerous! But finally I forced to work with it in “Archdaily Inspector” project seriously. So I developed a code to convert a string into a valid file name.

This code is written in Action Script but regular expressions can be used in other programming languages. This function is removing invalid characters like (~ \ / : * ? ” < > |) and trimming spaces from beginning and ending of input string.

[btcode title=”AS3″ lang=”java”]
function GetValidFileName( str:String ) : String
str = str.replace( /[~\\\/:\*\?”<>\|]/g , “” );
str = str.replace( /^\s+|\s+$/gs , “” );
return str;