JavaScript Replace Last Occurrence

JavaScript Replace Last Occurrence | This article will show you how to replace the last occurrence of a character or string in a given string in JavaScript. One, or more matches of a pattern can sometimes be replaced by a replacement in the new string that the replace() method returns.

The replacement can be a string or a function performed for each match, and the pattern can be either a string or a RegExp. Only the initial instance of a pattern in a string will be changed. The first string is not altered.

Pattern:- a string or an object containing a symbol might both be used.
The replace approach is a regular expression and a common illustration. Any value without the Symbol.replace() method will have its value forced to be a string.

Replacement:- can be a function or a string.
If it’s a string, it will swap out the portion of the string that matches the pattern. A variety of unique replacement patterns are supported; for more information, see the section below on specifying a string as the replacement.

If it’s a function, it will be called for each match and the result will be the text to replace. The section below, Specifying a function as the replacement, describes the arguments given to this function. 

The string value that this method is called on is not altered in any way. It gives back a fresh string.

Only one replacement will be made to a string pattern. Use replaceAll() or a regular expression with the g flag to execute a global search and replace.

JavaScript Replace Last Occurrence Using replace() Method

The target string and replacement are passed as arguments when calling the Symbol.replace() method of an object with the name pattern, including RegExp objects. The return value of replace becomes its return value (). In this instance, the @@replace method completely encodes the behavior of replace(); for instance, any reference to “capturing groups” in the following description refers to functionality offered by RegExp.prototype[@@replace].

The replacement is prepended to the beginning of the string if the pattern is an empty string.

Defining the regular expression in replace()

The ignore case flag and regular expression are defined in replace() in the example that follows.

const str = 'This is Know Program 2025';
const newstr = str.replace(/2025/i, '2035');
console.log(newstr);

Output:-

This is Know Program 2035

Using the global and ignoreCase flags with replace()

Only regular expressions can be used to perform global replacements. The following example uses a regular expression that enables replace() to swap out every instance of the word “east” in the string for the word “west.”

const re = /Bits/gi;
const str = 'Riz studies in Bits, Sid studies in Bits';
const newstr = str.replace(re, 'ABC');
console.log(newstr);

Output:-

Riz studies in ABC, Sid studies in ABC

To replace a substring with a new one and return a new string, use the replace() method. To replace a substring with a different one anywhere it occurs use a regular expression with the global flag (g).

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

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