Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript - Replace all non-alpha numeric characters except period

I need to replace all non alpha-numeric characters in a file name except for the period, I've been searching around and found close answers but not exact, here is what I narrowed it down to:

var temp = originalname.replace(/\W+/g, "_");

But this replaces everything, how can I exclude the period here (or any other characters if possible)?

like image 738
Mankind1023 Avatar asked Oct 16 '25 22:10

Mankind1023


1 Answers

You can use a negated character class:

var temp = originalname.replace(/[^\w.]+/g, "_");

[^\w.]+ will match 1 or more of any character that is not a word character and not a DOT.

like image 115
anubhava Avatar answered Oct 19 '25 11:10

anubhava