Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>return filename.split('.').pop(); </code></pre> <p>Keep it simple :) </p> <p><strong>Edit:</strong></p> <p>This is another non-regex solution that I believe is more efficient:</p> <pre><code>return filename.substring(filename.lastIndexOf('.')+1, filename.length) || filename; </code></pre> <p>There are some corner cases that are better handled by <a href="https://stackoverflow.com/a/12900504/1249581">VisioN's answer</a> below, particularly files with no extension (<code>.htaccess</code> etc included).</p> <p>It's very performant, and handles corner cases in an arguably better way by returning <code>""</code> instead of the full string when there's no dot or no string before the dot. It's a very well crafted solution, albeit tough to read. Stick it in your helpers lib and just use it.</p> <p><strong>Old Edit:</strong></p> <p>A safer implementation if you're going to run into files with no extension, or hidden files with no extension (see VisioN's comment to Tom's answer above) would be something along these lines</p> <pre><code>var a = filename.split("."); if( a.length === 1 || ( a[0] === "" &amp;&amp; a.length === 2 ) ) { return ""; } return a.pop(); // feel free to tack .toLowerCase() here if you want </code></pre> <p>If <code>a.length</code> is one, it's a visible file with no extension ie. <em>file</em></p> <p>If <code>a[0] === ""</code> and <code>a.length === 2</code> it's a hidden file with no extension ie. <em>.htaccess</em></p> <p>Hope this helps to clear up issues with the slightly more complex cases. In terms of performance, I believe this solution is <a href="https://stackoverflow.com/a/12900504/1249581">a little slower than regex</a> in most browsers. However, for most common purposes this code should be perfectly usable.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload