Javascript javascript中用于文件路径验证的正则表达式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2030285/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 22:28:37  来源:igfitidea点击:

Regex for file path validation in javascript

javascriptregexfilepath

提问by postalservice14

I can't seem to find a Regular Expression for JavaScript that will test for the following cases:

我似乎找不到可以测试以下情况的 JavaScript 正则表达式:

  • c:\temp
  • D:\directoryname\testing\
  • \john-desktop\tempdir\
  • c:\临时
  • D:\目录名\测试\
  • \约翰桌面\临时目录\

You can see what I am going for. I just need it to validate a file path. But it seems like all the expressions I have found don't work for JavaScript.

你可以看到我要做什么。我只需要它来验证文件路径。但似乎我发现的所有表达式都不适用于 JavaScript。

回答by Kiran Kumar

Here is the Windows path validation, it's works fine for all windows path rules.

这是 Windows 路径验证,它适用于所有 Windows 路径规则。

var contPathWin = document.editConf.containerPathWin.value;

if(contPathWin=="" || !windowsPathValidation(contPathWin))
{
    alert("please enter valid path");
    return false;
}

function windowsPathValidation(contwinpath)
{
    if((contwinpath.charAt(0) != "\" || contwinpath.charAt(1) != "\") || (contwinpath.charAt(0) != "/" || contwinpath.charAt(1) != "/"))
    {
       if(!contwinpath.charAt(0).match(/^[a-zA-Z]/))  
       {
            return false;
       }
       if(!contwinpath.charAt(1).match(/^[:]/) || !contwinpath.charAt(2).match(/^[\/\]/))
       {
           return false;
       } 

}

and this is for linux path validation.

这是用于 linux 路径验证。

var contPathLinux = document.addSvmEncryption.containerPathLinux.value;

if(contPathLinux=="" || !linuxPathValidation(contPathLinux))
{
    alert("please enter valid path");
    return false;
}

function linuxPathValidation(contPathLinux)
{
    for(var k=0;k<contPathLinux.length;k++){
        if(contPathLinux.charAt(k).match(/^[\]$/) ){
            return false;
        }
    }
    if(contPathLinux.charAt(0) != "/")
    {
        return false;
    }
    if(contPathLinux.charAt(0) == "/" && contPathLinux.charAt(1) == "/")
    {
        return false;
    }
    return true;
}

Try to make it in a single condition.

尝试在单一条件下进行。

回答by Gaim

Try this:

尝试这个:

([a-zA-Z]:)?(\[a-zA-Z0-9_-]+)+\?

EDIT:

编辑:

@Bart made me think about this regexp. This one should work nice for windows' paths.

@Bart 让我想到了这个正则表达式。这个应该很适合 Windows 的路径。

^([a-zA-Z]:)?(\[^<>:"/\|?*]+)+\?$

回答by Danubian Sailor

I've found an example here: http://regexlib.com/Search.aspx?k=file+name&AspxAutoDetectCookieSupport=1and I've modified it to match pathes starting with '\':

我在这里找到了一个例子: http'\': //regexlib.com/Search.aspx?k=file+name&AspxAutoDetectCookieSupport=1 我已经修改了它以匹配以开头的路径:

^((([a-zA-Z]:|\)\)|(\))?(((\.)|(\.\.)|([^\/:\*\?"\|<>\. ](([^\/:\*\?"\|<>\. ])|([^\/:\*\?"\|<>]*[^\/:\*\?"\|<>\. ]))?))\)*[^\/:\*\?"\|<>\. ](([^\/:\*\?"\|<>\. ])|([^\/:\*\?"\|<>]*[^\/:\*\?"\|<>\. ]))?$

This is clear-regex unescaped version with is easier to read (really!).

这是一个清晰的正则表达式非转义版本,更容易阅读(真的!)。

It matches pathes like:

它匹配以下路径:

  • c:\temp
  • \temp
  • ..\my folder
  • ü??
  • c:\临时
  • \temp
  • ..\我的文件夹
  • ??

etc.

等等。

回答by Kevin Peno

I think this will work:

我认为这会奏效:

var reg = new RegExp("^(?>[a-z]:)?(?>\|/)?([^\/?%*:|\"<>\r\n]+(?>\|/)?)+$", "i");

I've just excluded all(?) invalid characters in filenames. Should work with international filenames (I dunno) as well as any OS path type (with the exceptions noted below).

我刚刚排除了文件名中的所有()无效字符。应该使用国际文件名(我不知道)以及任何操作系统路径类型(除了下面提到的例外)。

回答by Rubens Farias

You could start with:

你可以从:

^([a-zA-Z]:)?(\[a-zA-Z0-9_\-]+)+\?

This matches all your samples.

这匹配您的所有样本。