Why does my javascript.replace () not work?

I am trying to delete any character except 0-9 az AZ ....

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

is there any obvious reason why the above is not working?

+5
source share
2 answers

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

file_name = file_name.replace(/[^A-Z0-9\._\-]/gi, '');
+13
source

Give Regexr a go. Hover over the checkboxes in the text box at the top to see the options you have. Look at the bottom panel to see the generated regular expression.

0
source

All Articles