Get string between last dash and last dot using string lastIndexOf method - Javascript String

Javascript examples for String:indexOf

Description

Get string between last dash and last dot using string lastIndexOf method

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
    window.onload=function(){//  w w w  .j a  v a 2 s.  c o m
var test = "http://foo.com/bar/image/abc.jpg";
var i = test.lastIndexOf("/");
var j = test.lastIndexOf(".");
var str = test.substring(i + 1, j);
document.body.innerHTML += "<p>" + str + "</p>";
    }

      </script> 
   </head> 
   <body>  
   </body>
</html>

Related Tutorials