Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
117
rated 0 times [  123] [ 6]  / answers: 1 / hits: 32497  / 7 Years ago, mon, february 13, 2017, 12:00:00

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?


More From » regex

 Answers
11

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);




[#58953] Friday, February 10, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
brandt

Total Points: 43
Total Questions: 90
Total Answers: 111

Location: Aruba
Member since Fri, Jun 24, 2022
2 Years ago
brandt questions
Sun, Jul 4, 21, 00:00, 3 Years ago
Wed, Jun 30, 21, 00:00, 3 Years ago
Sat, Jan 23, 21, 00:00, 3 Years ago
Mon, Sep 21, 20, 00:00, 4 Years ago
;