2

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
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)? Commented Jun 14, 2011 at 14:18

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I can't use that regex in PHP.
@medilies Sure, but that is also to be expected. The OP asked for a regexp for JavaScript, not PHP.
3

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.

Comments

0

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.

3 Comments

What, no RFC link containing the regexp?
@Matt: I must be feeling really lazy. :)
@Muhammed: Do you mean, write it for you? That's probably at least an hour's work writing, testing, etc.; I'm not in a position to do it for you. Instead, you're better off using the link that @Matt helpfully provided to the syntax definitions and delving into JavaScript's regular expressions syntax, crafting it yourself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.