Skip to main content
dublove
Legend
June 30, 2025
Answered

To remove all forbidden characters from a filename, what do I write with just one regular?

  • June 30, 2025
  • 1 reply
  • 337 views

All forbidden characters:

? \ |":*/<>/ \r \n

 

Why doesn't this work:

myName.replace(/[\?\*\/\:\|\<\r\n\>\"\\]/,'')

 

And separating one at a time works:

myName.replace(/\? /, '').replace(/\r/, '').replace(/\*/, '');

 

Correct answer dublove

Hi m1b.

Thank you very much.
You contribute so much to the forum.

 

I went straight to regular.
You're missing a \.

            .replace(/\.(jpe*g)|(tif)|(psd)$/g, '')
            .replace(/[\.\/\\:\*\?"<>\|\`\x00-\x1F]|^\s*|\s*$/g, '');

1 reply

m1b
Community Expert
Community Expert
June 30, 2025

Hi @dublove how about this?

/**
 * Returns a cleaned file name by removing invalid characters.
 * 
 * Illegal Characters:
 *   /   Path separator (Unix, macOS)
 *   \   Path separator (Windows)
 *   :   Reserved on Windows (drive letter separator) and older macOS versions
 *   *   Wildcard
 *   ?   Wildcard
 *   "   Quotation mark, not allowed in Windows filenames
 *   <   Redirect operator, disallowed in Windows
 *   >   Redirect operator, disallowed in Windows
 *   |   Pipe, used for command chaining in shells, not allowed in Windows filenames
 *   `   Backtick, often used in shell commands and can cause confusion, best avoided
 * 
 * Also excludes:
 *   \x00-\x1F  Control characters, usually invisible
 *   ^\s*       Leading whitespace
 *   \s*$       Trailing whitespace
 * 
 * @author m1b
 * @version 2025-06-30
 * @param dirtyName - the file name to clean.
 * @returns {String?} - the cleaned file name.
 */
function cleanFileName(dirtyName) {

    var cleanName = dirtyName.replace(/[\/\\:\*\?"<>\|\`\x00-\x1F]|^\s*|\s*$/g, '');
    if (cleanName.length > 0)
        return cleanName

};
dublove
dubloveAuthorCorrect answer
Legend
June 30, 2025

Hi m1b.

Thank you very much.
You contribute so much to the forum.

 

I went straight to regular.
You're missing a \.

            .replace(/\.(jpe*g)|(tif)|(psd)$/g, '')
            .replace(/[\.\/\\:\*\?"<>\|\`\x00-\x1F]|^\s*|\s*$/g, '');
m1b
Community Expert
Community Expert
June 30, 2025

Hi @dublove well done.

 

This is just for your information; because I think you don't realize the benefit of making functions. When you write a function—and keep it available for use in future scripts—it means you (a) can rely on it correctly doing its single job—and nothing else!—and (b) you don't have to re-invent the wheel each time you write a script, and (c) gives you a place to add documentation, (d) makes it portable—you can easily add it to a "library" file and use it in any project that needs it in the future, and (e) it makes code much easier to understand and maintain.

 

Yesterday I wrote a fairly involved script in very short time because it relied on a function that I wrote 8 years ago and have polished ever since, most recently editing about one year ago. Using this function I didn't have to worry about whether it would work—I can rely on it, and the script was easy to write.

 

Do not be fooled into thinking that shorter code is better—it isn't. It almost never matters how long the code is—and dense code is harder to read and less likely to include good documentation. Functions make code clearer and easier to understand and debug. For example, if you see a function called "cleanFileName" you don't need to look at the regular expression, just the name, to know what it does.

 

Yay functions!