简体   繁体   English

文件和目录正则表达式

[英]file and directory regex

I am trying to create a regEx for file and directory path validation. 我正在尝试创建用于文件和目录路径验证的regEx。 I have implemented this, but its failing 1 of the conditions, that it should not allow ie multiple slashes together. 我已经实现了这一点,但是它没有达到条件之一,即它不允许多个斜杠放在一起。 Also, no other special character should not be allowed 另外,不允许使用其他特殊字符

var x = /^(\\|\/){1}([a-zA-Z0-9\s\-_\@\-\^!#$%&]*?(\\|\/)?)+(\.[a-z\/\/]+)?$/i
  • test 1 -> / (should pass) 测试1-> /(应通过)
  • test 2 -> /asdf (should pass) 测试2-> / asdf(应通过)
  • test 3 -> /asdf/scd.csv (should pass) 测试3-> /asdf/scd.csv(应通过)
  • test 4 -> //asdf (should fail, currently passing) 测试4-> // asdf(应该失败,当前通过)
  • test 5 -> /asd/ads/c.csv/ (should pass) 测试5-> /asd/ads/c.csv/(应通过)
  • test 6 -> asd/asfd/a (should fail) 测试6-> asd / asfd / a(应该失败)

Can suggestion how to solve this? 可以建议如何解决吗?

The path //asdf is valid on LINUX, UNIX, iOS, and Android, so your code already works. 路径//asdf在LINUX,UNIX,iOS和Android上有效,因此您的代码已经可以使用。 However, if it is important for some reason to invalidate that particular set of valid paths, simply substitute a plus sign in place of the an asterisk after the [az...] character group. 但是,如果出于某种原因使那组有效路径无效很重要,则只需在[az ...]字符组之后用加号代替星号即可。 That will cause invalidation of multiple path separators with no intervening characters. 这将导致没有中间字符的多个路径分隔符失效。

It is probably useful to comment on larger issues with the regex approach and details. 对正则表达式方法和详细信息的较大问题进行评论可能很有用。

1) You can use [\\/] instead of (\\|/), however both will allow false positives on every combination of operating system and file system. 1)您可以使用[\\ /]代替(\\ | /),但是两者都将允许对操作系统和文件系统的每种组合进行误报。 (Those that require forward slash should exclude backslashes as a separator and vice versa.) (那些需要使用正斜杠的用户应将反斜杠作为分隔符,反之亦然。)

2) The character range [a-zA-Z0-9\\s-_\\@-\\^!#$%&] in the question is not the permissible character range for directory path elements for any known combination of operating system and file system. 2)问题中的字符范围[a-zA-Z0-9 \\ s -_ \\\\ @-\\ ^!#$%&]不是操作系统和文件的任何已知组合的目录路径元素允许的字符范围系统。 For instance, a period is valid in directory names for most. 例如,在大多数目录名称中,句点有效。

3) Permissible character ranges are not portable. 3)允许的字符范围不可移植。 (The most reliable way to test path validation is to touch the file name on the actual file system, meaning actually instantiate an empty file and capture any indications of instantiation failure.) (测试路径验证的最可靠方法是触摸实际文件系统上的文件名,这意味着实际实例化一个空文件并捕获实例化失败的任何指示。)

4) You don't want or need a question mark after your asterisk or after your second (\\|/) group. 4)在星号之后或第二组(\\ | /)之后,您不需要或不需要问号。 They don't create a bug, but they waste either compilation or run time, and they obfuscate your regex purpose. 它们不会产生错误,但是会浪费编译时间或运行时间,并且会混淆您的正则表达式目的。

5) You also need to repeat the character range just before the extension or rearrange like the example below. 5)您还需要在扩展或重新排列之前重复字符范围,如下例所示。

6) You don't need to add the AZ range to the az range if you use \\i as a flag at the end of the regex. 6)如果在正则表达式的末尾使用\\ i作为标志,则无需将AZ范围添加到az范围。

7) It appears from the list of desired results that relative paths are to be filtered out, but there is no explicit mention of that as a rule for the solution. 7)从期望结果的列表中看来,相对路径将被过滤掉,但是作为解决方案的规则,没有明确提及。

With hesitation, this code is provided to demonstrate a few of the above improvements. 毫不犹豫地提供了此代码来演示上述一些改进。

 // This code is not production worthy // for reasons (1) through (3) given // above and is provided only for the // purpose of clarifying points made. var re = /^([\\\\/][a-z0-9\\s\\-_\\@\\-\\^!#$%&]*)+(\\.[az][a-z0-9]+)?$/i console.log( [ '/', '/asdf', '/asdf/scd.csv', '//asdf', '/asd/ads/c.csv/', 'asd/asfd/a' ].map(RegExp.prototype.test, re)) 

Try using /^(\\/|([\\\\/][\\w\\s@^!#$%&-]+)+(\\.[az]+[\\\\/]?)?)$/i instead, which forces at least one character to match between each slash: 尝试使用/^(\\/|([\\\\/][\\w\\s@^!#$%&-]+)+(\\.[az]+[\\\\/]?)?)$/i而是强制每个斜线之间至少匹配一个字符:

 var regex = /^(\\/|([\\\\/][\\w\\s@^!#$%&-]+)+(\\.[az]+[\\\\/]?)?)$/i console.log([ '/', '/asdf', '/asdf/scd.csv', '//asdf', '/asd/ads/c.csv/', 'asd/asfd/a' ].map(RegExp.prototype.test, regex)) 

((\/[\w\s\.@^!#$%&-]+)+\/?)|\/[\w\.\s@^!#$%&-]*

This was tested to match your sample input, 经过测试以匹配您的样本输入,
BUT on np++ (ie perl-regex flavor), because I have no experience with javascript. 但是在np ++上(即perl-regex风格),因为我没有使用javascript的经验。
Therefor here the same in flavor-indpendent prose. 因此,这里与风味无关的散文相同。

"(slash and character many times, followed by optional slash) “(多次斜杠和字符,后跟可选的斜杠)
or 要么
slash and zero or more characters". 斜线和零个或多个字符”。

Note1: I added explicit "." 注意1:我添加了显式的“。” to allowed characters. 允许的字符。
Note2: I assume your "\\/" means, "explicit slash, not backslash". 注意2:我假设您的“ \\ /”表示“显式斜杠,而不是反斜杠”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM