I've tried to find a windows file path validation for Javascript, but none seemed to fulfill the requirements I wanted, so I decided to build it myself.
The requirements are the following:
Here is the regex I came up with: /^([a-z]:((\|/|\\|//))|(\\|//))[^<>:"|?*]+/i
But there are some issues:
var reg = new RegExp(/^([a-z]:((\\|\/|\\\\|\/\/))|(\\\\|\/\/))[^<>:"|?*]+/i);
var startList = [
'C://test',
'C://te?st.html',
'C:/test',
'C://test.html',
'C://test/hello.html',
'C:/test/hello.html',
'//test',
'/test',
'//test.html',
'//10.1.1.107',
'//10.1.1.107/test.html',
'//10.1.1.107/test/hello.html',
'//10.1.1.107/test/hello',
'//test/hello.txt',
'/test/html',
'/tes?t/html',
'/test.html',
'test.html',
'//',
'/',
'\\\\',
'\\',
'/t!esrtr',
'C:/hel**o'
];
startList.forEach(item => {
document.write(reg.test(item) + ' >>> ' + item);
document.write("<br>");
});
Unfortunately, JavaScript flavour of regex does not support lookbehinds, but fortunately it does support lookaheads, and this is the key factor how to construct the regex.
Let's start from some observations:
After a dot, slash, backslash or a space there can not occur another
dot, slash or backslash. The set of "forbidden" chars includes also
\n
, because none of these chars can be the last char of the file name
or its segment (between dots or (back-)slashes).
Other chars, allowed in the path are the chars which you mentioned
(other than ...), but the "exclusion list" must include also a dot,
slash, backslash, space and \n
(the chars mentioned in point 1).
After the "initial part" (C:\) there can be multiple instances of char mentioned in point 1 or 2.
Taking these points into account, I built the regex from 3 parts:
+
quantifier).So the regex is as follows:
^
- Start of the string.(?:[a-z]:)?
- Drive letter and a colon, optional.[\/\\]{0,2}
- Either a backslash or a slash, between 0 and 2 times.(?:
- Start of the non-capturing group, needed due to the +
quantifier after it.
[.\/\\ ]
- The first alternative.(?![.\/\\\n])
- Negative lookahead - "forbidden" chars.|
- Or.
[^<>:"|?*.\/\\ \n]
- The second alternative.)+
- End of the non-capturing group, may occur multiple times.$
- End of the string.If you attempt to match each path separately, use only i
option.
But if you have multiple paths in separate rows, and match them
globally in one go, add also g
and m
options.
For a working example see https://regex101.com/r/4JY31I/1
Note: I suppose that !
should also be treated as a forbidden
character. If you agree, add it to the second alternative, e.g. after *
.
This may work for you: ^(?!.*[\\\/]\s+)(?!(?:.*\s|.*\.|\W+)$)(?:[a-zA-Z]:)?(?:(?:[^<>:"\|\?\*\n])+(?:\/\/|\/|\\\\|\\)?)+$
You have a demo here
Explained:
^
(?!.*[\\\/]\s+) # Disallow files beginning with spaces
(?!(?:.*\s|.*\.|\W+)$) # Disallow bars and finish with dot/space
(?:[a-zA-Z]:)? # Drive letter (optional)
(?:
(?:[^<>:"\|\?\*\n])+ # Word (non-allowed characters repeated one or more)
(?:\/\/|\/|\\\\|\\)? # Bars (// or / or \\ or \); Optional
)+ # Repeated one or more
$
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With