替换除句号外的所有非字母数字字符

时间:2021-04-09 09:33:53

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)?

但这取代了一切,我怎么能排除这里的周期(或任何其他字符,如果可能)?

1 个解决方案

#1


3  

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.

[^ \ w。+将匹配1个或多个不是单词字符的字符,而不是一个点。

#1


3  

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.

[^ \ w。+将匹配1个或多个不是单词字符的字符,而不是一个点。