开发者

regular expression for physical path

can someone tell me the javascript regular expression for physical path like

1) User Should enter something like this in the textbox( c://Folder1/) . Maybe in d: or e:

2) But after that acceptable

a) (c://Folder1/Folder2/)

b) (d://Folder1/Folder2/Folder3/开发者_Go百科abc.txt)

e) (c://Folder1/Folder2/Folder3/abc.txt)


From the examples you've given, something like this should work:

[a-zA-Z]://(\w+/)+

ie:
[a-zA-Z] = a single letter (upper or lower case)
followed by
:// = the characters "://"
followed by:
(\w+/)+ = at least one "something/".
"something/" defined as :
\w+ = at least one word character (ie any alphanumeric), followed by
/ = literal character "/"

Hope this helps - my syntax may be a little off as I'm not fully up to speed on the javascript variant for regex.

Edit: put regex in code tags so it is visible! And tidy up explanation.


This problem is actually trickier than you think. You're trying to validate a path, but paths can be surprisingly hard to properly validate. Are you properly handling UNC network paths, e.g.?

This is known as the canonicalization problem and is part of writing secure code. I suggest checking out some guidance from Microsoft for properly canonicalizing and validating the path in your application. The advantage of canonicalizing your path is that you also implicitly validate its format because the canonical form will be returned from a library call that will only return paths that are potentially valid (properly formatted). This means that you don't have to do any sort of regex validation at all. Just throw your string at the method that canonicalizes the path (Path.GetFullPath() probably) and handle the exception for an invalid path.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜