javascript中的文件路径验证 [英] File path validation in javascript

查看:132
本文介绍了javascript中的文件路径验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在javascript中验证XML文件路径。我的REGEX是:

I am trying to validate XML file path in javascript. My REGEX is:

var isValid = /^([a-zA-Z]:)?(\\{2}|\/)?([a-zA-Z0-9\\s_@-^!#$%&+={}\[\]]+(\\{2}|\/)?)+(\.xml+)?$/.test(str);

即使路径错误,它也会返回true。
这些是有效路径

It returns true even when path is wrong. These are valid paths

D:/test.xml
D:\\folder\\test.xml
D:/folder/test.xml
D:\\folder/test.xml
D:\\test.xml


推荐答案

首先出现明显错误:

+ 是一个重复指标,含义至少为一个。

所以(\.xml +)将匹配以 .xm 开头的所有内容,后跟一个或多个 l (它也匹配 .xmlllll )。 表示是可选的,所以(\.xml +)?的含义是 .xml 但不是必需的。

+ 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.

相同([a-zA-Z] :)?这意味着司机信是可选的。

the same is for ([a-zA-Z]:)? this means the driver letter is optional.

现在不是那么明显的错误

[a-zA-Z0-9 \\\\ _ @ - ^!#$%& + = {} \ [\]] 在这里定义允许的字符列表。你有 \\\\ 我假设你想允许空格,但这允许 \ s 因此您需要将其更改为 \s 。然后你有这个部分 @ - ^ 我假设你想要允许 @ - ^ - 中有特殊含义[ ] 用它来定义一个范围,以便允许所有字符在 @ ^ 如果你想允许 - 你需要在那里转义它,所以你必须写 @ \ - ^ 你还需要注意 ^ ,如果它正好在之后[ 它也有特殊意义。

[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.

你的正则表达式应该包含以下部分:


  • ^ [az]:以( ^ )司机信件

  • ((\\ | \ /)[a-z0-9 \ _s_ @ \ - ^!#$ %& + = {} \ [\]] +)+ 后跟一个或多个以 \ 开头的路径部分或 / 并且路径名包含一个或多个已定义的字母( a-z0-9 \ _s_ @ \ - ^!#$%& + = {} \ [\]

  • \.xml $ 以( $ .xml

  • ^[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

因此你的最终正则表达式应如下所示

/ ^ [az ]:((\\ | \ /)[A-z0-9\s_ @ \ - ^#$%&安培; + = {} \ [\!]] +)+ \。 xml $ / i.test(str)

(假设您使用 i 标志执行不区分大小写的正则表达式)

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)

编辑:

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

更新:

你需要注意 / \ 如果你需要逃避它们取决于是否你使用它与新的RegExp('...正则表达式...',我)新的RegExp(...... regex ...,i)或者 / ...正则表达式... / i

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

有关正则表达式的更多信息,您应该查看例如 www.regular-expressions.info

for further informations about regular expressions you should take a look at e.g. www.regular-expressions.info

这篇关于javascript中的文件路径验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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