33

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?

Kit
  • 724
  • 4
  • 11
user3378165
  • 6,546
  • 17
  • 62
  • 101
  • How do you determine "illegal" character? Your regex says that `.`,`_` and many other characters are "illegal", Why? – Toto Feb 13 '17 at 17:55
  • 3
    Just add the space to the class `[^a-z0-9 ]` that way it won't get matched (removed). –  Feb 13 '17 at 19:20

4 Answers4

72

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);
ibrahim mahrir
  • 31,174
  • 5
  • 48
  • 73
9

You are searching for all non numeric and roman letters. If you want to exclude the space from being replaced:

Just add a space to the selection to exclude :)

str= str.replace(/([^a-z0-9 ]+)/gi, '-');
// add a space here -------^
Pineda
  • 7,435
  • 3
  • 30
  • 45
  • Sorry but I don't think I understand what you mean, could you explain? – user3378165 Feb 13 '17 at 17:49
  • Your regexp looks to match characters that are not: lowercase characters from a-z, uppercase from A-Z and numbers from 0-9, and replaces the match. To avoid replacing spaces, adding a space as I've recommended will achieve what you want because you're trying to match anything _**not**_ included between the `[ .. ]` square brackets – Pineda Feb 13 '17 at 17:50
9

You can use \W+

\W (non-word-character) matches any single character that doesn't match by \w (same as [^a-zA-Z0-9_])
Source: https://www.ntu.edu.sg/home/ehchua/programming/howto/Regexe.html

str = str.replace(/(\W+)/gi, '-');
user2226755
  • 12,494
  • 5
  • 50
  • 73
2

Add the '\s' whitespace to your exclusion.

str = str.replace(/([^a-z0-9\s]+)/gi, '-');
Kit
  • 724
  • 4
  • 11