Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why isn't my javascript .replace() working?

Tags:

javascript

I'm trying to remove any character except 0-9 a-z A-Z....

var file_name = file.name;
file_name = file_name.replace(/[^A-Z0-9\._\-]/i, '');

any obvious reason the above isn't working?

like image 808
Haroldo Avatar asked Sep 07 '10 11:09

Haroldo


1 Answers

You need to specify the global-flag on your regular expression. Otherwise, only the first occurrence will be replaced:

file_name = file_name.replace(/[^A-Z0-9\._\-]/gi, '');
like image 105
jwueller Avatar answered Oct 06 '22 00:10

jwueller