从文件名中删除非法字符但保留空格 [英] Remove illegal characters from a file name but leave spaces

查看:36
本文介绍了从文件名中删除非法字符但保留空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码行来删除文件名中的非法字符:

I have the following line of code to remove illegal characters from a file name:

str= str.replace(/([^a-z0-9]+)/gi, '-');

这工作正常,但它也删除了空格,我怎样才能只删除非法字符而保留空格?

That works fine but it also removes the spaces, how can I only remove the illegal characters but leave the spaces?

推荐答案

列出非法字符 在这里.要替换它们,请使用此正则表达式 /[/\\?%*:|"<>]/g 像这样:

Illegal characters are listed here. To replace them use this regex /[/\\?%*:|"<>]/g like this:

var filename = "f?:i/le>  n%a|m\\e.ext";

filename = filename.replace(/[/\\?%*:|"<>]/g, '-');

console.log(filename);

这篇关于从文件名中删除非法字符但保留空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆