0

How can I split the ending of a file, like for instance I want file01 instead of file01.xlsx

My written code:

var filename = $v('P40_UPLOAD').split('\\').pop().split('/').pop();

var s = $s("P40_NAME",filename);

s.split('.')[0];

Unfortunately it doens't work.

wayne
  • 81
  • 1
  • 11

3 Answers3

0

If you know the length of the extension, you can use x.slice(0, -4) (where 4 is the three characters of the extension and the dot).

Manish Vadher
  • 1,416
  • 14
  • 14
0

Try the following:

var file = "file01.xlsx";

var index = file.lastIndexOf(".");
 var filename = file.substring(0,index);
 console.log(filename);
amrender singh
  • 7,430
  • 3
  • 20
  • 27
0

Do this Regex:

var url = 'http://stackoverflow.com/file01.xlsx';
var filename = url.match( /([^\/]+)(?=\.\w+$)/ )[0];
console.log( filename )

Alternately, you can do this without regular expressions altogether, by finding the position of the last / and the last . using lastIndexOf and getting a substring between those points:

var url = 'http://stackoverflow.com/file01.xlsx';
var filename = url.substring(url.lastIndexOf( '/' ) + 1, url.lastIndexOf( '.' ));
console.log( filename )
Kavian K.
  • 1,282
  • 1
  • 8
  • 11