Match file name and file extension from one regular expression

I'm sure it should be easy, but I'm afraid ...

var regexFileName = /[^\\]*$/; // match filename var regexFileExtension = /(\w+)$/; // match file extension function displayUpload() { var path = $el.val(); //This is a file input var filename = path.match(regexFileName); // returns file name var extension = filename[0].match(regexFileExtension); // returns extension console.log("The filename is " + filename[0]); console.log("The extension is " + extension[0]); } 

The function above works fine, but I'm sure that it can be achieved with a single regular expression, referring to different parts of the array returned by the .match () method. I tried combining this regex, but to no avail.

In addition, I do not use the string to check it in the example, because console.log () hides the backslash in the file path, and it confused me :)

+6
source share
6 answers

Assuming all files have the extension, you can use

 var regexAll = /[^\\]*\.(\w+)$/; 

Then you can do

 var total = path.match(regexAll); var filename = total[0]; var extension = total[1]; 
+5
source

/^.*\/(.*)\.?(.*)$/g after this first group will be your file name, and the second group will be the extension.

 var myString = "filePath/long/path/myfile.even.with.dotes.TXT"; var myRegexp = /^.*\/(.*)\.(.*)$/g; var match = myRegexp.exec(myString); alert(match[1]); // myfile.even.with.dotes alert(match[2]); // TXT 

This works even if your file name contains more than one dot or does not contain periods (has no extension).
EDIT:
This is for linux, for Windows it is /^.*\\(.*)\.?(.*)$/g (in the linux separator directory / in windows \ )

+6
source

You can use groups in your regular expression for this:

 var regex = /^([^\\]*)\.(\w+)$/; var matches = filename.match(regex); if (matches) { var filename = matches[1]; var extension = matches[2]; } 
+2
source

This even recognizes /home/someUser/.aaa/.bb.c :

 function splitPathFileExtension(path){ var parsed = path.match(/^(.*\/)(.*)\.(.*)$/); return [parsed[1], parsed[2], parsed[3]]; } 
+2
source

I think this is the best approach as it only matches the actual directory, file names and extension. and also groups the path, file name and file extension. It also works with empty paths only with the file name.

 ^([\w\/]*?)([\w\.]*)\.(\w)$ 

Testing

 the/p0090Aath/fav.min.icon.png the/p0090Aath/fav.min.icon.html the/p009_0Aath/fav.m45in.icon.css fav.m45in.icon.css favicon.ico 

Exit

 [the/p0090Aath/][fav.min.icon][png] [the/p0090Aath/][fav.min.icon][html] [the/p009_0Aath/][fav.m45in.icon][css] [][fav.m45in.icon][css] [][favicon][ico] 
0
source

I know this is an old question, but here is another solution that can handle several points in the name, and also when there is no extension at all (or the extension is only "."):
/^(.*?)(\.[^.]*)?$/

Taking this apiece at the same time:
^
Anchor to the beginning of the line (to avoid partial matches)

(.*?)
Match any character . , 0 or more times * , lazily ? (don't just grab them all if the subsequent optional extension can match) and put them in the first capture group ( ) ,

(\.
Run the second capture group to expand with ( . This group starts with a literal character . (With which we run away with \ , so . Is not interpreted as "match any character").

[^.]*
Define the character set [] . Match non-character characters by specifying this is an inverted character set ^ . Match 0 or more characters not . to get the rest of the file extension * . We specify it in such a way that it does not match file names like foo.bar.baz , incorrectly providing an extension with more than one dot in it .bar.baz instead of just .baz . . no escaping inside [] is necessary since everything (except ^ ) is a literal in the character set.

)?
Complete the second capture group ) and indicate that the entire group is optional ? since it may not have an extension.

$
Anchor to the end of the line (again, to avoid partial matches)

If you use ES6, you can use destruction to get the results in 1 line:
[,filename, extension] = /^(.*?)(\.[^.]*)?$/.exec('foo.bar.baz'); which gives the file name as 'foo.bar' and the extension as '.baz' .
'foo' gives 'foo' and ''
'foo.' gives 'foo' and '.'
'.js' gives '' and '.js'

0
source

Source: https://habr.com/ru/post/906845/


All Articles