简体   繁体   中英

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.

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

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

file names will exist with some dll or exe extensions.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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