We have a requirement to remove some illegal character in file name like
slash etc… So I put on a regex in our code:
I assume the above regex will match everything that is not number/char/dot/dash/underscore.
Turns out I was wrong! The
- is special case inside
[...] that is used for range. It should be in the beginning or in the last or escaped. Otherwise it will match all the character that is in between . and _ in ASCII character set. So in my case, the
.-_ part will try to match characters from 46(
ASCII char table.
The correct one should be putting it in the last
[^a-zA-Z0-9._-] . Or just escape it: