相关疑难解决方法(0)

为什么 Powershell Test-Path -Isvalid 总是返回 true

根据文档 -IsValid 应该检查路径的语法是否正确。我为测试路径放置的任何内容它总是返回 true

指示此 cmdlet 测试路径的语法,无论路径的元素是否存在。如果路径语法有效,则此 cmdlet 返回 $True;如果无效,则返回 $False

$val = Test-Path "sadfasdasdfasdf" -IsValid
Run Code Online (Sandbox Code Playgroud)

powershell

2
推荐指数
1
解决办法
1213
查看次数

正则表达式Windows路径验证器

我试图找到Java脚本的Windows文件路径验证,但似乎没有一个可以满足我想要的要求,因此我决定自己构建它。

要求如下:

  • 路径不能为空
  • 可以以x:\,x:\\,\,//开头,然后是文件名(无需文件扩展名)
  • 文件名不能包含以下特殊字符:<>:“ |?*
  • 文件名不能以点或空格结尾

这是我想出的正则表达式: / ^([az]:((\ | / | \\\ ///))|(\\ | //))[^ <>:“ |?*] + /一世

但是有一些问题:

  • 它还会验证包含规则中提到的特殊字符的文件名
  • 它不包含最后一条规则(不能以。或空格结尾)

var reg = new RegExp(/^([a-z]:((\\|\/|\\\\|\/\/))|(\\\\|\/\/))[^<>:"|?*]+/i);
var startList = [
  'C://test',
  'C://te?st.html',
  'C:/test',
  'C://test.html',
  'C://test/hello.html',
  'C:/test/hello.html',
  '//test',
  '/test',
  '//test.html',
  '//10.1.1.107',
  '//10.1.1.107/test.html',
  '//10.1.1.107/test/hello.html',
  '//10.1.1.107/test/hello',
  '//test/hello.txt',
  '/test/html',
  '/tes?t/html',
  '/test.html',
  'test.html',
  '//',
  '/',
  '\\\\',
  '\\',
  '/t!esrtr',
  'C:/hel**o'
];

startList.forEach(item => {
  document.write(reg.test(item) + '  >>>   ' + item);
  document.write("<br>");
});
Run Code Online (Sandbox Code Playgroud)

javascript regex validation

2
推荐指数
1
解决办法
818
查看次数

标签 统计

javascript ×1

powershell ×1

regex ×1

validation ×1