Sanitize a string to be safe for use as a file name in Windows and Unix
systems by stripping all control
characters and
restricted characters (\/:*?"<>|
).
// Some string that may be unsafe as a filesystem filename
var UNSAFE_FILENAME = "h*ello:/world?\u0000";
// Sanitize the unsafe filename to be safe for use as a filename
var sanitize = require("sanitize-filename"),
filename = sanitize(UNSAFE_FILENAME);
// Create a file using the safe filename
require("fs").createWriteStream(filename).end();
Sanitize the input string, filename
, and replace unsafe characters
with optional replacement. The options.replacement
can be a string to
replace unsafe characters with.
npm install sanitize-filename