I have the following line of code to remove illegal characters from a file name:
str= str.replace(/([^a-z0-9]+)/gi, '-');
That works fine but it also removes the spaces, how can I only remove the illegal characters but leave the spaces?
Cause of the Error MessageThis error occurs when a hyperlink is added which contains special characters.
Do not use the following reserved names for the name of a file: CON, PRN, AUX, NUL, COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9, LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, and LPT9. Also avoid these names followed immediately by an extension; for example, NUL. txt is not recommended.
Illegal characters are listed here. To replace them use this regex /[/\\?%*:|"<>]/g
like this:
var filename = "f?:i/le> n%a|m\\e.ext";
filename = filename.replace(/[/\\?%*:|"<>]/g, '-');
console.log(filename);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With