用于验证文件名的 Javascript 正则表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11100821/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript regex for validating filenames
提问by Rafael Sedrakyan
I have a regexp to validate file names. Here is it:
我有一个正则表达式来验证文件名。就这个:
/[0-9a-zA-Z\^\&\'\@\{\}\[\]\,$\=\!\-\#\(\)\.\%\+\~\_ ]+$/
It should allow file names like this:
它应该允许这样的文件名:
aaa
aaa.ext
a#
A9#.ext
The following characters are not allowed \ / : * ? \" < > |
不允许使用以下字符 \ / : * ? \" < > |
The problem is that file names like *.txt
or /\kk
passes the validation. I am doing validation with keyup event. So when I put one extra character after not allowed one it shows that everything is correct.
问题是文件名像*.txt
或/\kk
通过了验证。我正在使用 keyup 事件进行验证。因此,当我在不允许的字符之后添加一个额外的字符时,它表明一切都是正确的。
采纳答案by Mattias Buelens
You need to add a starting anchor:
您需要添加一个起始锚点:
/^[0-9a-zA-Z ... ]+$/
This tells the engine to match from the start of the input all the way to the end of the input, whereas for your original expression it only needs to match at the end of the input.
这告诉引擎从输入的开始一直匹配到输入的结尾,而对于您的原始表达式,它只需要在输入的末尾匹配。
回答by Andrew D.
For Windows names.
对于 Windows 名称。
var isValid=(function(){
var rg1=/^[^\/:\*\?"<>\|]+$/; // forbidden characters \ / : * ? " < > |
var rg2=/^\./; // cannot start with dot (.)
var rg3=/^(nul|prn|con|lpt[0-9]|com[0-9])(\.|$)/i; // forbidden file names
return function isValid(fname){
return rg1.test(fname)&&!rg2.test(fname)&&!rg3.test(fname);
}
})();
isValid('file name');
回答by Qtax
You need to anchor the expression by using ^
and $
. For example:
您需要使用^
和来锚定表达式$
。例如:
/^[-\w^&'@{}[\],$=!#().%+~ ]+$/
Note that you need to escape -
in a character class, or place it first/last.
请注意,您需要-
在字符类中转义,或者将其放在第一个/最后一个。
回答by Ryan Williams
/^(?!\.)(?!com[0-9]$)(?!con$)(?!lpt[0-9]$)(?!nul$)(?!prn$)[^\|\*\?\:<>/$"]*[^\.\|\*\?\:<>/$"]+$/
Must not be empty.
Must not start with .
Must not be com0-com9, con, lpt0-lpt9, nul, prn
Must not contain | * ? \ : < > $
Must not end with .
回答by user2173353
I would try something with this Regex (you can even make a validation attribute for ASP.NET MVC with it!):
我会用这个 Regex 尝试一些东西(你甚至可以用它为 ASP.NET MVC 创建一个验证属性!):
@"^[^\u0022\u003C\u003E\u007C\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007\u0008\u0009\u000A\u000B\u000C\u000D\u000E\u000F\u0010\u0011\u0012\u0013\u0014\u0015\u0016\u0017\u0018\u0019\u001A\u001B\u001C\u001D\u001E\u001F\u003A\u002A\u003F\u005C\u002F]*$"
If it matches the input, it is valid filename (at least on Windows).
如果它与输入匹配,则它是有效的文件名(至少在 Windows 上)。
回答by lale liley
Since this post (regex-for-windows-file-name)redirects to this question, I assume its about windows file names.
由于这篇文章 (regex-for-windows-file-name)重定向到这个问题,我假设它是关于 Windows 文件名的。
And based on the comment and referenceby @Leon on the answer by @AndrewD, I made this regular expression and it works for me.
根据@Leon 对@AndrewD 的回答的评论和参考,我制作了这个正则表达式,它对我有用。
/^(con|prn|aux|nul|com[1-9]|lpt[1-9])$|([<>:"\/\|?*])|(\.|\s)$/ig
According to the naming-conventions (see reference above), I agree that "com0" should be a valid file name, but its not working if you try to name a file "com0" on windows, so I guess thats missing in the article.
根据命名约定(请参阅上面的参考资料),我同意“com0”应该是一个有效的文件名,但是如果您尝试在 Windows 上将文件命名为“com0”,则它不起作用,所以我想这在文章中是缺失的.
So this regular expression would be more safe
所以这个正则表达式会更安全
/^(con|prn|aux|nul|com[0-9]|lpt[0-9])$|([<>:"\/\|?*])|(\.|\s)$/ig