javascript javascript中的文件路径验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16231210/
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
File path validation in javascript
提问by Muhammad Imran Tariq
I am trying to validate XML file path in javascript. My REGEX is:
我正在尝试在 javascript 中验证 XML 文件路径。我的正则表达式是:
var isValid = /^([a-zA-Z]:)?(\{2}|\/)?([a-zA-Z0-9\s_@-^!#$%&+={}\[\]]+(\{2}|\/)?)+(\.xml+)?$/.test(str);
It returns true even when path is wrong. These are valid paths
即使路径错误,它也会返回 true。这些是有效的路径
D:/test.xml
D:\folder\test.xml
D:/folder/test.xml
D:\folder/test.xml
D:\test.xml
回答by t.niese
At first the obvious errors:
起初明显的错误:
+
is a repeat indicator that has the meaning at least one.
so the (\.xml+)
will match everything starting with .xm
followed by one or more l
(it would also match .xmlllll
). the ?
means optional, so (\.xml+)?
has the meaning it could have an .xml
but it is not required.
+
是具有至少一个含义的重复指示符。
所以(\.xml+)
将匹配以.xm
开头的所有内容,然后是一个或多个l
(它也会匹配.xmlllll
)。该?
方法可选的,所以(\.xml+)?
有这意味着它可以有一个.xml
,但它不是必需的。
the same is for ([a-zA-Z]:)?
this means the driver letter is optional.
同样是([a-zA-Z]:)?
这种手段的驱动器盘符是可选的。
Now the not so obvious errors
现在不那么明显的错误
[a-zA-Z0-9\\s_@-^!#$%&+={}\[\]]
here you define a list of allowed chars. you have \\s
and i assume you want to allow spaces, but this allows \
and s
so you need to change it to \s
. then you have this part @-^
i assume you want to allow @
, -
and ^
but the -
has a special meaning inside of [
]
with it you define a range so you allow all chars that are in the range of @
to ^
if you want to allow -
you need to escape it there so you have to write @\-^
you also need to take care about ^
, if it is right after the [
it would have also a special meaning.
[a-zA-Z0-9\\s_@-^!#$%&+={}\[\]]
在这里你定义了一个允许的字符列表。你有\\s
,我假设你想允许空格,但这允许\
,s
所以你需要将其更改为\s
. 那么你有这部分@-^
我假设你想允许@
,-
并^
而是-
有特殊的含义里面的[
]
它定义了一个范围,因此允许在范围内的所有字符 @
来^
,如果你想要让-
你需要逃避它有所以你要写@\-^
你也需要照顾^
,如果是对的后面[
那也有特殊的意义。
your Regex should contain the following parts:
您的正则表达式应包含以下部分:
^[a-z]:
start with (^
) driver letter((\\|\/)[a-z0-9\s_@\-^!#$%&+={}\[\]]+)+
followed by one or more path parts that start with either\
or/
and having a path name containing one or more of your defined letters (a-z0-9\s_@\-^!#$%&+={}\[\]
)\.xml$
ends with ($
) the.xml
^[a-z]:
以 (^
) 驱动程序字母开头((\\|\/)[a-z0-9\s_@\-^!#$%&+={}\[\]]+)+
后跟一个或多个以\
或开头的路径部分,/
路径名包含一个或多个您定义的字母 (a-z0-9\s_@\-^!#$%&+={}\[\]
)\.xml$
以 ($
)结尾.xml
therefore your final regex should look like this/^[a-z]:((\\|\/)[a-z0-9\s_@\-^!#$%&+={}\[\]]+)+\.xml$/i.test(str)
(under the assumption you do a case insensitve regex using the i
flag)
因此,您的最终正则表达式应如下所示/^[a-z]:((\\|\/)[a-z0-9\s_@\-^!#$%&+={}\[\]]+)+\.xml$/i.test(str)
(假设您使用该i
标志执行不区分大小写的正则表达式)
EDIT:
编辑:
var path1 = "D:/test.xml"; // D:/test.xml
var path2 = "D:\folder\test.xml"; // D:\folder\test.xml
var path3 = "D:/folder/test.xml"; // D:/folder/test.xml
var path4 = "D:\folder/test.xml"; // D:\folder/test.xml
var path5 = "D:\test.xml"; // D:\test.xml
console.log( /^[a-z]:((\|\/)[a-z0-9\s_@\-^!#$%&+={}\[\]]+)+\.xml$/i.test(path1) );
console.log( /^[a-z]:((\|\/)[a-z0-9\s_@\-^!#$%&+={}\[\]]+)+\.xml$/i.test(path2) );
console.log( /^[a-z]:((\|\/)[a-z0-9\s_@\-^!#$%&+={}\[\]]+)+\.xml$/i.test(path3) );
console.log( /^[a-z]:((\|\/)[a-z0-9\s_@\-^!#$%&+={}\[\]]+)+\.xml$/i.test(path4) );
console.log( /^[a-z]:((\|\/)[a-z0-9\s_@\-^!#$%&+={}\[\]]+)+\.xml$/i.test(path5) );
UPDATE:
更新:
you need to take care about the /
and the \
if you need to escape them depends on if you use it with new RegExp(' ... the regex ... ',"i")
and new RegExp(" ... the regex ... ","i")
or with / ... the regex ... /i
你需要照顾/
和\
如果你需要逃避他们取决于你是否使用它new RegExp(' ... the regex ... ',"i")
和new RegExp(" ... the regex ... ","i")
或/ ... the regex ... /i
for further informations about regular expressions you should take a look at e.g. www.regular-expressions.info
有关正则表达式的更多信息,您应该查看例如www.regular-expressions.info
回答by Ejaz
This could work out for you
这对你有用
var str = 'D:/test.xml';
var str2 = 'D:\folder\test.xml';
var str3 = 'D:/folder/test.xml';
var str4 = 'D:\folder/test.xml';
var str5 = 'D:\test\test\test\test.xml';
var regex = new RegExp('^[a-z]:((\\|\/)[a-zA-Z0-9_ \-]+)+\.xml$', 'i');
regex.test(str5);
The reason of having \\\\
in RegExp to match a \\
in string is that javascript uses \
to escape special characters, i.e., \n
for new lines, \b
for word boundary etc. So to use a literal \
, use \\
. It also allows you to have different rules for file name and folder name.
其原因\\\\
在正则表达式匹配一个\\
字符串是JavaScript使用\
特殊字符进行转义,即,\n
新的线路,\b
一个字边界等,所以使用文字\
,使用\\
。它还允许您对文件名和文件夹名有不同的规则。
Update
更新
[a-zA-Z0-9_\-]+
this section of regexp actually match file/folder name. So to allow more characters in file/folder name, just add them to this class, e.g., to allow a *
in file/folder name make it [a-zA-Z0-9_\-\*]+
[a-zA-Z0-9_\-]+
这部分正则表达式实际上匹配文件/文件夹名称。因此,要允许在文件/文件夹名称中包含更多字符,只需将它们添加到此类,例如,允许*
在文件/文件夹名称中使用[a-zA-Z0-9_\-\*]+
Update 2
更新 2
For adding to the answer, following is an RegExp that adds another check to the validation, i.e., it checks for mixing of /
and \\
in the path.
为了添加到答案,下面是一个 RegExp,它向验证添加了另一个检查,即它检查路径中的/
和 的混合\\
。
var str6 = 'D:/This is folder/test @ file.xml';
var str7 = 'D:/This is invalid\path.xml'
var regex2 = new RegExp('^[a-z]:(\/|\\)([a-zA-Z0-9_ \-]+\1)*[a-zA-Z0-9_ @\-]+\.xml?', 'gi');
regex2
will match all paths but str7
regex2
将匹配所有路径,但 str7
Update
更新
My apologies for mistyping a ?
instead of $
in regex2
. Below is the corrected and intended version
我很抱歉输入错误 a?
而不是$
in regex2
。以下是更正后的预期版本
var regex2 = new RegExp('^[a-z]:(\/|\\)([a-zA-Z0-9_ \-]+\1)*[a-zA-Z0-9_ @\-]+\.xml$', 'i');
回答by Ravi Thapliyal
Tested using Scratchpad.
使用 Scratchpad 测试。
var regex = /^[a-z]:((\/|(\?))[\w .]+)+\.xml$/i;
Prints true
in Web Console: (Ctrl+Shift+K on Firefox)
true
在 Web 控制台中打印:(Firefox 上的 Ctrl+Shift+K)
console.log(regex.test("D:/test.xml"));
console.log(regex.test("D:\folder\test.xml"));
console.log(regex.test("D:/folder/test.xml"));
console.log(regex.test("D:\folder/test.xml"));
console.log(regex.test("D:\test.xml"));
console.log(regex.test("D:\te st_1.3.xml")); // spaces, dots allowed
Or, using Alert boxes:
或者,使用警报框:
alert(regex.test("D:/test.xml"));
alert(regex.test("D:\folder\test.xml"));
alert(regex.test("D:/folder/test.xml"));
alert(regex.test("D:\folder/test.xml"));
alert(regex.test("D:\test.xml"));
alert(regex.test("D:\te st_1.3.xml"));
Invalid file paths:
无效的文件路径:
alert(regex.test("AD:/test.xml")); // invalid drive letter
alert(regex.test("D:\\folder\test.xml")); // three backslashes
alert(regex.test("/folder/test.xml")); // drive letter missing
alert(regex.test("D:\folder/test.xmlfile")); // invalid extension