How do I remove spaces and special characters from the file name during rendering?

10-11  Source: Network gathering  Views:6 

I understand that I can set LR_renamingTokensOn to true, but I would like to replace all spaces in the file name with an underscore and remove characters not in the range A-Z and 0-9. What's the easiest way to achieve this?
local photo = catalog:getTargetPhoto()
local sesn = LrExportSession {
    photosToExport = { photo },
    exportSettings = {
        -- ... (determine from export preset) - whatev you want, just be sure you set export directory: LR_export_destinationPathPrefix
        LR_tokens = "{{custom_token}}",
        LR_tokenCustomString = LrPathUtils.removeExtension( photo:getFormattedMetadata( 'fileName' ) ):gsub( "[ %c]", "" ) -- remove spaces and control characters
Related articles