Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6022707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:53:49+00:00 2026-05-23T03:53:49+00:00

I have a varable var file = "testfile.txt"; I am using the following script

  • 0

I have a varable var file = "testfile.txt"; I am using the following script to find its extension and it works well:

var parts = file.split('.');
var flename = parts[0];
var ext = parts[1];

But if i have a filename with more than one dots, the logic sucks.. It will give txt for filename.txt, but not in the case of file.nam.e.txt or something else.

Anybody suggest me the simplest solution to do this using JavaScript and/or jQuery?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T03:53:50+00:00Added an answer on May 23, 2026 at 3:53 am

    Simple as that:

    var ext = parts[parts.length - 1];
    

    And as for the file name:

    var flename = parts.slice(0, parts.length - 1).join(".");
    

    Last but not least, I would also validate before that block of code that file is not empty to avoid exceptions.

    Edit: another similar way that is more elegant:

    var ext = parts.splice(-1).join();
    var flename = parts.join(".");
    

    Note that unlike the first approach, this will actually change the array parts so don’t use it afterwards.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have used this code var dest1 = File.AppendText(Path.Combine(_logFolderPath, log1.txt)); dest1.WriteLine(line.Trim()); to write to
I have a javascript file called pendingAjaxCallsCounter.js with a variable var pendingAjaxCalls which is
I have a array that I am writing to a file using var_export() .
I have the following code which loads a xml file to populate a dropdown.
I have a jquery script that searches a php file that produces an XML
I have a flash file where I can pass xml from file : script
I have this form to upload an xml file to server, I am using
I have a flv video file. I loaded the binary data of this flv
I have a for loop, and inside it a variable is assigned with var.
I have a small problem, I want to load data from a PHP file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.