0

I have to parse strings that present name of files as:

myfilename.txt
anotherfilename.jpg
a.filename.with.dots.jpg

I need to extract the filename without it's extension. for example "myfilename.txt" should return "myfilename" and "a.filename.with.dots.jpg" should return "a.filename.with.dots" not that some filename are problematic in what they contain and the javascript should be able to handle this.

what's the best was of doing this? this is what I tried so far and didn't work.

namearr = name.split('.')
filename = namearr.splice()

I can use regex like

^(.?)..?$

for this but it seems like an overkill for this

the problem I see with other answered for this is that they don't take in account the number of dots the filename can contain. (not excluding the extension)

john_black
  • 167
  • 3
  • 14
  • Possible duplicate of [How to trim a file extension from a String in JavaScript?](https://stackoverflow.com/questions/4250364/how-to-trim-a-file-extension-from-a-string-in-javascript) – ShaH Jun 26 '18 at 11:54
  • 1
    Please search your title before asking – mplungjan Jun 26 '18 at 11:55
  • 1
    `name="a.filename.with.dots.jpg"; filenamename=name.substring(0,name.lastIndexOf("."))` – mplungjan Jun 26 '18 at 11:58
  • this is what worked for me: function removeExt(name){ namearr = name.split('.'); namearr.pop(); return namearr.join('.'); } – john_black Jun 26 '18 at 13:52

0 Answers0