ArticleZip > Javascript Replace Last Occurrence Of Text In A String

Javascript Replace Last Occurrence Of Text In A String

When working with JavaScript, knowing how to replace the last occurrence of text in a string can come in handy in various scenarios. This functionality can be quite useful when you need to update specific content within a string without affecting other occurrences of the same text. In this article, we'll walk you through a simple and efficient way to achieve this using JavaScript.

To replace the last occurrence of text in a string, we can utilize a combination of methods provided by JavaScript. One approach to solve this involves using the `lastIndexOf()` method to identify the position of the last occurrence of the text we want to replace, and then employing the `slice()` method along with string concatenation to replace the desired text.

Let's dive into an example to demonstrate how this can be implemented in practice:

Javascript

function replaceLastOccurrence(inputString, searchText, replacementText) {
  const index = inputString.lastIndexOf(searchText);
  if (index === -1) {
    return inputString; // Text not found
  }
  
  return inputString.slice(0, index) + replacementText + inputString.slice(index + searchText.length);
}

// Example Usage
const originalString = "This is a sample text with sample content.";
const newText = replaceLastOccurrence(originalString, "sample", "updated");

console.log(newText);

In the code snippet above, we define a function `replaceLastOccurrence` that takes three parameters: the input string, the search text we want to replace, and the replacement text. The function first finds the index of the last occurrence of the search text using `lastIndexOf()`. If the search text is not found in the string, the function simply returns the original input string.

Next, the function constructs the new string by concatenating three parts: the substring before the last occurrence, the replacement text, and the substring following the last occurrence of the search text.

By using this approach, you can efficiently replace the last occurrence of text in a given string, ensuring that only the intended part of the text is modified while leaving other occurrences untouched.

It's worth noting that this method is case-sensitive, so ensure that the search text matches the case in the original string. Additionally, this solution assumes a single instance of the search text in the input string. If you need to handle multiple occurrences, you may need to adapt the logic accordingly.

In conclusion, mastering the ability to replace the last occurrence of text in a string using JavaScript can enhance your productivity while working with string manipulation tasks. With the approach outlined in this article, you now have a practical method to efficiently update text in a string as needed. Experiment with this technique in your projects to see how it can streamline your development process and make your code more dynamic.

×