开发者

validation of UNC path using javascript

开发者 https://www.devze.com 2023-03-13 08:42 出处:网络
How can I validate a UNC path using javascript? Examples of valid UNC path are :- \\\\192.168.0.100\\MuhammedRaufK开发者_运维问答\\Share

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


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.com/Forums/TechOff/132283-regex-UNC-share

It suggests a (well-documented) RegEx of

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

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


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.


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.

0

精彩评论

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