简体   繁体   中英

Javascript RegEx for a comma delimited list of file paths

I am using a third party .NET upload control that provides client side function that returns the list of files as a string; however, the string is comma delimited. Since a comma is valid in filenames it could present an issue (an edge case, I guess you'd call it).

So I am trying to use RegEx to parse out each file name. Prior I had been doing

var files = strFromControlCall.split(",").map(function (str) { return str.replace(/^.*[\\\/]/, ''); });

But as noted, the split on the comma results in an actual file name being split further.

The RegEx I am currently working on is as follows:

var getFilesRegex = function(path) {
   var m = path.match(/[^\\]*\.([\.\w]+)/g);
   return {
      files: m
   };
};

note using term "dot" for a . as it's hard to see, in my opinion, outside of codeblocks

the use case here is full file paths coming in. eg, "driveletter:\\temp\\test\\test.zip" This is able to match files with multiple "dots" (for instance filename.aspx.js) and a comma, among others, but the issue I am having now is that you can also have a "dot" in a foldername. I've tried adding . to the first bracket in the regex: [^\\.] but that fails the tests as well.

How can I get this to work? I realize the above RegEx likely has other issues (for example, I believe extension can have other characters than what \\w allows, etc) and don't mind having them pointed out as well.

Ideally, the control would use a delimiter not valid in a filename (such as \\ / : * ? " < > |), but I have no way to change that...

jsFiddle link - note i commented out the alert to avoid popups on load...

As long as a comma can mean either a separator or a filename character, there is indeed no way to reliably cover the original filenames. A regexp can't do any more here than a plain .split() .

Anything you did to try to recover from the case of a comma in a filename could only be a guess. For example, if the start of the first path were a drive letter or UNC ( /(\\\\|([AZ]:)?)\\\\/ ), you could check the start of each of the following paths to see if any of those don't begin with the drive letter pattern, and if so assume it's a bad split and recombine with the previous path and a comma.

But for file uploads on the web you can't assume much of anything about the pathnames you get. Some browsers won't put a full path in a file upload field value at all, only the file part (which would prevent you from doing the above check); path segments may contain multiple dots and commas in any order (including no dots at all), and Mac and Unix machines have different path separators entirely.

So whilst it would be best to request a fix of the third-party code, and if not then maybe you could consider grabbing the onchange event of each upload control and preventing any upload with a comma in the filename... actually you should avoid relying on upload filenames in general, and fall back to something sensible rather than abruptly failing when a filename format is not what you expect.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM