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)

4
  • Possible duplicate of How to trim a file extension from a String in JavaScript? Commented Jun 26, 2018 at 11:54
  • 1
    Please search your title before asking Commented Jun 26, 2018 at 11:55
  • 1
    name="a.filename.with.dots.jpg"; filenamename=name.substring(0,name.lastIndexOf(".")) Commented Jun 26, 2018 at 11:58
  • this is what worked for me: function removeExt(name){ namearr = name.split('.'); namearr.pop(); return namearr.join('.'); } Commented Jun 26, 2018 at 13:52

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.