string - validation of UNC path using javascript - Stack Overflow

How can I validate a UNC path using javascript?Examples of valid UNC path are :-\192.168.0.100Muhamme

How can I validate a UNC path using javascript?

Examples of valid UNC path are :-

\\192.168.0.100\MuhammedRaufK\Share

\\Shared1_svr\Shared1\WGroups\Network\Orders.xls

How can I validate a UNC path using javascript?

Examples of valid UNC path are :-

\\192.168.0.100\MuhammedRaufK\Share

\\Shared1_svr\Shared1\WGroups\Network\Orders.xls
Share Improve this question asked Jun 14, 2011 at 14:15 mmk_openmmk_open 1,0355 gold badges18 silver badges28 bronze badges 1
  • What do you mean by validate? Do you want to actually check that the file/directory exists? Server-side JS or client (browser) side? If the latter, which browser(s)? – Matt Ball Commented Jun 14, 2011 at 14:18
Add a ment  | 

3 Answers 3

Reset to default 4

Using MSDN as a reference, here a regular expression to capture various parts of a UNC path:

/^\\\\([^\\:\|\[\]\/";<>+=,?* _]+)\\([\u0020-\u0021\u0023-\u0029\u002D-\u002E\u0030-\u0039\u0040-\u005A\u005E-\u007B\u007E-\u00FF]{1,80})(((?:\\[\u0020-\u0021\u0023-\u0029\u002D-\u002E\u0030-\u0039\u0040-\u005A\u005E-\u007B\u007E-\u00FF]{1,255})+?|)(?:\\((?:[\u0020-\u0021\u0023-\u0029\u002B-\u002E\u0030-\u0039\u003B\u003D\u0040-\u005B\u005D-\u007B]{1,255}){1}(?:\:(?=[\u0001-\u002E\u0030-\u0039\u003B-\u005B\u005D-\u00FF]|\:)(?:([\u0001-\u002E\u0030-\u0039\u003B-\u005B\u005D-\u00FF]+(?!\:)|[\u0001-\u002E\u0030-\u0039\u003B-\u005B\u005D-\u00FF]*)(?:\:([\u0001-\u002E\u0030-\u0039\u003B-\u005B\u005D-\u00FF]+)|))|)))|)$/

which is broken down as follows:

/^\\\\ - match a string that begins with two backward slashes \\\\

() - capture (1) (host name)

[^\\:\|\[\]\/";<>+=,?* _]+ - match any sequence of characters, excluding \\:\|\[\]\/";<>+=,?* _, one or more times

\\ - match a literal backward slash \\

() - capture (2) (share name)

[\u0020-\u0021\u0023-\u0029\u002D-\u002E\u0030-\u0039\u0040-\u005A\u005E-\u007B\u007E-\u00FF]{1,80} - match any sequence of 1 to 80 characters matching !#$%'()\-\.0-9@A-Z^_`a-z{}~ and Latin-1 Unicode supplement

( - begin capture (3) (object name)

( - begin capture (4) (path name)

(?:\\[\u0020-\u0021\u0023-\u0029\u002D-\u002E\u0030-\u0039\u0040-\u005A\u005E-\u007B\u007E-\u00FF]{1,255})+? - capture but do not remember a \\ literal followed by one or more sequences of 1 to 255 characters matching !#$%'()\-\.0-9@A-Z^_`a-z{}~ and Latin-1 Unicode supplement and do so non-greedily (5)

|) - OR capture nothing (4) (path name)

(?: - begin capture but do not remember (6)

\\ - match a \\ literal

( - begin capture (7) (file name)

(?:[0-9a-z]{1,255}){1} - capture but do not remember a sequence of 1 to 255 characters matching !#$%'()\+,\-\.0-9;=@A-Z\[\]^_`a-z{ (8)

(?: - begin capture but do not remember (9)

\:(?=[\u0001-\u002E\u0030-\u0039\u003B-\u005B\u005D-\u00FF]|\:) - match a literal : only if followed by \u0001-\u002E\u0030-\u0039\u003B-\u005B\u005D-\u00FF OR a literal :

(?: - begin capture but do not remember (10)

([\u0001-\u002E\u0030-\u0039\u003B-\u005B\u005D-\u00FF]+(?!\:)|[\u0001-\u002E\u0030-\u0039\u003B-\u005B\u005D-\u00FF]*) - capture a sequence of one or more characters not followed by a literal :; otherwise, capture a sequence of 0 or more characters (11) (stream name)

(?: - begin capture but do not remember (12)

\: - match a literal :

([\u0001-\u002E\u0030-\u0039\u003B-\u005B\u005D-\u00FF]+) - capture a sequence of one or more characters (13) (stream type)

|) - OR capture nothing (12)

) - end capture (10)

|) - OR capture nothing (9)

) - end capture (7) (file name)

) - end capture (6)

|) - OR capture nothing (3) (object name)

$/ - end of string

A few notes:

  1. The regular expression matches an entire string.
  2. Both a host name and a share name are required in order for this regular expression to match.
  3. Host name matching is not rigorous (specified via 4 RFCs: 3986, 1035, 1123, and 4291). Hence, some false positives may occur. If the host name needs to be rigorously verified, capture the host name and test independently of this regular expression.
  4. A trailing \\ is never permitted.

For a JavaScript library implementation, including tests and examples, see here.

This is an old question, but one I needed the answer to recently. There are some suggestions on this Stack Overflow question (Regex to validate a network path it PHP, jQuery, JavaScript, Ruby), but most of them fail to account for many of the allowed variations in UNC paths. Further research led me to this thread on channel9:

https://channel9.msdn./Forums/TechOff/132283-regex-UNC-share

It suggests a (well-documented) RegEx of

^(\\\\[^/\\\]\[":;|<>+=,?* _]+\\[^/\\\]\[":;|<>+=,?*]+)((?:\\[^\\/:*?"<>|]+)*\\?)$

which appears to work well, at least for my needs.

If by "validate" you mean "ensure it's in the correct format," sure. UNC paths are regular enough to be defined by a regular expression.

If you mean that and testing whether the path references a valid directory or file, that will depend on the capabilities of the host environment in which the JavaScript is running. You couldn't readily do it from most browsers, for instance, but you could with NodeJS or Rhino or Windows Script Host.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744787469a4593714.html

相关推荐

  • string - validation of UNC path using javascript - Stack Overflow

    How can I validate a UNC path using javascript?Examples of valid UNC path are :-\192.168.0.100Muhamme

    10小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信