简体   繁体   English

给定文件路径的正则表达式

[英]regular expression for given file path

This is the most recent segment of regexp i tried....but every time it is giving -1,I want it to match with my string str....I have started with basic file path,it should return any value greater than -1 if matched. 这是我尝试过的正则表达式的最新部分。...但是每次给出-1时,我都希望它与我的字符串str匹配。...我已经从基本文件路径开始,它应该返回更大的值如果匹配,则返回-1。

var str=" c:\folder1\ ";
var n;
var y=/^[a-z]\:\\(folder1)?\\$/g;
n=str.search(y);
alert(n);

I want to write it for specific folder names...it should accept both the string given below... example 我想为特定的文件夹名称编写它...它应该接受下面给出的两个字符串...示例

File C:\\Program Files (x86)\\Webdeveloper\\dfg.dll 文件C:\\ Program Files(x86)\\ Webdeveloper \\ dfg.dll

File C:\\Program Files\\Webdeveloper\\dfg.exe 文件C:\\ Program Files \\ Webdeveloper \\ dfg.exe

file names will exist with some dll or exe extensions. 文件名将带有某些dll或exe扩展名。

/[c]{1}[:]{1}\\{1}(folder1){1}\\/ shows match for folder1 when i am using http://regex101.com/#javascript,I am passing the string "file c:\\folder1\\ is corrupted" . / [c] {1} [:] {1} \\ {1}(folder1){1} \\ /在我使用http://regex101.com/#javascript时显示文件夹1的匹配项,我正在传递字符串“文件c:\\ folder1 \\已损坏”。 but it is not working for whole path string. 但不适用于整个路径字符串。

Thanks in advance! 提前致谢!

You have trailing whitespaces in your source string, so your regexp won't match. 您的源字符串中包含尾随空格,因此您的正则表达式将不匹配。 Additionally, you should add slashes to your source string to make it capturable. 此外,应在源字符串中添加斜杠以使其可捕获。 change your code to: 将您的代码更改为:

var str=" c:\\folder1\\ ";
var n;
var y=/^\s*[a-z]\:\\(folder1)?\\\s*$/g;
str.match(y) // [" c:\folder1\ "]

\\f is a escape character which means formfeed. \\fescape character ,表示换页。

Use this regular expression 使用此正则表达式

^[C][:]\\[A-Z+a-z ()0-9\\]+[.][a-z]+$

Demo/Testing 演示/测试

Try this: 尝试这个:

here in every '\\\\',one \\ is escape character and other \\ is litral. 在每个“ \\\\”中,一个\\是转义字符,另一个\\是字母。

var str="c:\folder1\abc.txt";
var n;
var y=/^(c:\\folder1\\abc\.txt)$/;
n=str.search(y);
alert(n);

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

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