简体   繁体   中英

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

I'm trying to remove any character except 0-9 az AZ....

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

any obvious reason the above isn't working?

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

Give Regexr a go. Hover over the check boxes under the text box at the top to see the options you have. Look at the bottom pane to see the generated Regex.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM