-2

I have filename like this

var file= file[0].name.split(".");
var filename=file[file.length - 2];
var filetype=file[file.length - 1];

Issue is i get incomplete file name like text1.txt or 2002.txt

filename can be file.text1.txt or upload.file1.img.2002.txt

but i want complete filename like file.text1.txt or upload.file1.img.2002.txt

user3653474
  • 3,393
  • 6
  • 49
  • 135

3 Answers3

1

you can try it this way,

var file= file[0].name.split(".");
const extension = file.pop()
const filename = file.join('.')

console.log(extension, filename)

e.g.

const log = console.log;

var file= 'upload.file1.img.2002.txt'.split('.');
log('after split', file)

const extension = file.pop()
log('after pop', file)

const filename = file.join('.')

console.log('filename is ', filename, '\nextension is ', extension)
Vinod Liyanage
  • 945
  • 4
  • 13
1

Seems like you want to split the string by last ..

const filename = 'upload.file1.img.2002.txt';

const splitResult = filename.split(/\.(?!.*\.)/);

const [name, type] = splitResult;

console.log({name,type});

My approach is to find the dot . which is not followed by another dot (i.e. last dot in the string) and then split by this ..
As a result we'll get an array of two elements where first one would be the name and the last one would be file extension.

Jaood_xD
  • 808
  • 2
  • 4
  • 16
  • General rule of thumb: Regex is always the _last_ answer to your question! It's very CPU intensive and therefor any other solution that can get the job done should be preferred over using regex. – icecub Aug 29 '22 at 12:06
  • @icecub In general it's not very true and really depends on particular case, string length and regex complexity. For example I've benched my solution against the one Vinod Liyanage posted. As a result of 10kk runs mine was 1.5-2 times faster on average – Jaood_xD Aug 29 '22 at 12:21
  • Not to be unfounded, here's an example posted on [jsfiddle](https://jsfiddle.net/cbdw6nzy/) – Jaood_xD Aug 29 '22 at 12:27
  • 1
    Did a benchmark myself and your solution is in fact 15.32% faster, so I stand corrected there. I've always been taught not to use regex unless you really have no other option, but in my defense, that's 20 years old knowledge. Nevertheless, if proven wrong, I fully admit it :) – icecub Aug 29 '22 at 12:28
0

Here would be my approach, probably not the most efficient nor the most readable, but it gets the job done :-) I wrapped it inside a function for reusability. It assumes that we pass a filename without leading path as parameter.

function getFileInfo( filename ) {
    let arr = filename.split(".");
    if( arr.length < 2 ) return [ filename, "" ];
    return arr.reduce( (oldval, newval, currentIndex, arr) => {
        return currentIndex == arr.length - 1
        ? [ oldval, newval ]
        : oldval + "." + newval;
    });
}
let [ fname, ext ] = getFileInfo( "azet.sqdsd.fsdfsd.txt" );
// filename is now in variable fname and extension in variable ext

The way this works is it splits the filename on dots. Then it concatenates the strings in the array until it arrives on the last element where it returns an array with the filename and extension.

Special cases:

  • If no dots, it returns an array as [ filename, "" ]
  • If you pass a dot-file name, it returns an array as [ "", extension ]
Gabriel Hautclocq
  • 3,230
  • 2
  • 26
  • 31