• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / lastIndexOf JavaScript – Get the Last Position of a String in Another String

lastIndexOf JavaScript – Get the Last Position of a String in Another String

February 23, 2022 Leave a Comment

We can use the String lastIndexOf JavaScript method to get the last position of a value (string or character) within a string. If the value is not found within the string, -1 is returned.

"Text".lastIndexOf('x');

This would result in the following output:

2

Some other examples of the lastIndexOf JavaScript method are below:

var text = "This text is some Example text";
var str1 = text.lastIndexOf('e');
var str2 = text.lastIndexOf('E');
var str3 = text.lastIndexOf('text');
var str4 = text.lastIndexOf('t');
var str5 = text.lastIndexOf('Example');
var str6 = text.lastIndexOf('Examples');

Which would result in the following:

str1 = 27
str2 = 18
str3 = 26
str4 = 29
str5 = 18
str6 = -1

The main things to remember with the lastIndexOf() method are that the first character starts at index 0, not 1, and that it is case-sensitive.

An example using the lastIndexOf JavaScript Method

Below is an example of replacing an image using lastIndexOf and other JavaScript methods.

<img id="img1" src="http://theprogrammingexpert.com/wp-content/uploads/example-img1.png">
<div id="click-me">Change image</div>

Let’s say we have another image called “example-img2” which is in the same location as the image above. When we click the button, we want to swap out that image. We will use the lastIndexOf JavaScript method to get the location of “example-img1” and use the substring() method to get the first part of the image location. Then we will add the new image file name and replace the old image. Here is the JavaScript code below:

$("#click-me").click(function(){
  var oldImgAddress = $('#img1').attr('src');
  var indexOfImage = oldImgAddress.lastIndexOf('example-img1.png');
  var firstPartOfString = oldImgAddress.substring(0,indexOfImage);
  var newImgAddress = firstPartOfString + "example-img2.png"; // we add the new file name to the end of the string
  //This new string should now be "http://theprogrammingexpert.com/wp-content/uploads/example-img2.png"
  //We can now use jQuery to replace the old image  
  $('#img1').attr('src',newImgAddress);
});

The final code and output for this example is below:

Code Output:

Change image

Full Code:

<img id="img1" src="http://theprogrammingexpert.com/wp-content/uploads/example-img1.png">
<div id="click-me">Change image</div>

<script>

$("#click-me").click(function(){
  var oldImgAddress = $('#img1').attr('src');
  var indexOfImage = oldImgAddress.lastIndexOf('example-img1.png');
  var firstPartOfString = oldImgAddress.substring(0,indexOfImage);
  var newImgAddress = firstPartOfString + "example-img2.png";
  $('#img1').attr('src',newImgAddress);
});

</script>

Hopefully this article has been useful in helping you understand the String lastIndexOf() JavaScript method.

Other Articles You'll Also Like:

  • 1.  Using JavaScript to Check if Array is Empty
  • 2.  How to Convert Degrees to Radians Using JavaScript
  • 3.  Using JavaScript to Capitalize the First Letter of a String
  • 4.  Using JavaScript to Check if a Number is Divisible by 3
  • 5.  How to Use JavaScript to Round Up Numbers
  • 6.  innerHTML vs outerHTML
  • 7.  How to Remove Non Numbers From String in JavaScript
  • 8.  Using JavaScript to Square a Number
  • 9.  Using JavaScript to Add Item to Array if it Does Not Exist
  • 10.  Using JavaScript Math Module to Get Euler’s Constant e

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy