chris chris - 1 month ago 8
Javascript Question

Regex to validate a network path it PHP, jQuery, JavaScript, Ruby

Trying to figure out a regex for validating a network path ie:

\\comp\xyz
or
\\comp
or
\\comp\x\y\z\storage
or something much more lengthy on all parts but the jist of it is hopefully conveyed.

What I have currently is a simple input field, that I have a user passing information through. Thing of it is I don't want them to put it in wrong as the backend connects to a client that uses it to run processes on computers across a network, so last thing I need is someone mistyping something and being the cause of something else breaking. So I figure a quick preg_match will do me just fine in confirming this from the PHP side I am currently work with but I do have a need to work this into JavaScript and a layer thats built on Ruby so I not knowing if all regex is equal or not I need one that works on all levels..

Also to save me time later in coming back I will eventually need to figure out a regex that will allow a user to do either local path ie:
C:\
,
X:
,
H:\path\to\folder
or a network path as mentioned prior.

Answer

\\comp\x\y\z\storage:

/^\\(\\[^\s\\]+)+(\\)?$/

H:\path\to\folder:

/^([A-Za-z]:(\\)?|[A-Za-z]:(\\[^\s\\]+)+)(\\)?$/

Both:

/^(\\(\\[^\s\\]+)+|([A-Za-z]:(\\)?|[A-z]:(\\[^\s\\]+)+))(\\)?$/

Working demo: http://jsfiddle.net/DerekL/gLfdv/

Comments