Creating a legal filename in JavaScript

http://www.infinite-x.net/2007/04/29/creating-a-legal-filename-in-javascript/


I am working on a project this weekend ( yeah, I know ) where I will be creating a folder in the file system based on the Account Name for an Account within CRM.  Since it is possible to have characters within an Account Name that would be invalid in a file or folder name, they must be replaced.

The following JavaScript snippet will replace any invalid character within the Account Name with an underscore. 

// Create a dummy account name for testing purposes.
crmForm.all.name.DataValue = "this?is/\ a* test";

if (crmForm.all.name != null)
{
	// The replaceChar should be either a space
	// or an underscore.
	var replaceChar = "_";
	var regEx = new RegExp('[,/\:*?""<>|]', 'g');
	var Filename = crmForm.all.name.DataValue.replace(regEx, replaceChar);

	// Show me the new file name.
	alert(Filename);
}
Address: http://www.infinite-x.net/2007/04/29/creating-a-legal-filename-in-javascript/


my code:

    var filename1 = (widget.document.title ? widget.document.title : widget.document.URL);
    var regEx = new RegExp('[,/\:*?""<>|]', 'g');
    var filename = filename1.replace(regEx,"_");


你可能感兴趣的:(Creating a legal filename in JavaScript)