Javascript: Replace Last Occurrence of Text in a String

JavaScript: replace last occurrence of text in a string

Well, if the string really ends with the pattern, you could do this:

str = str.replace(new RegExp(list[i] + '$'), 'finish');

Replace last occurrence word in javascript

Here is an idea ....

This is a case-sensitive string search version

var str = 'abc def abc xyz';
var word = 'abc';
var newWord = 'test';

// find the index of last time word was used
// please note lastIndexOf() is case sensitive
var n = str.lastIndexOf(word);

// slice the string in 2, one from the start to the lastIndexOf
// and then replace the word in the rest
str = str.slice(0, n) + str.slice(n).replace(word, newWord);
// result abc def test xyz

If you want a case-insensitive version, then the code has to be altered. Let me know and I can alter it for you. (PS. I am doing it so I will post it shortly)

Update: Here is a case-insensitive string search version

var str = 'abc def AbC xyz';
var word = 'abc';
var newWord = 'test';

// find the index of last time word was used
var n = str.toLowerCase().lastIndexOf(word.toLowerCase());

// slice the string in 2, one from the start to the lastIndexOf
// and then replace the word in the rest
var pat = new RegExp(word, 'i')
str = str.slice(0, n) + str.slice(n).replace(pat, newWord);
// result abc def test xyz

N.B. Above codes looks for a string. not whole word (ie with word boundaries in RegEx). If the string has to be a whole word, then it has to be reworked.

Update 2: Here is a case-insensitive whole word match version with RegEx

var str = 'abc def AbC abcde xyz';
var word = 'abc';
var newWord = 'test';

var pat = new RegExp('(\\b' + word + '\\b)(?!.*\\b\\1\\b)', 'i');
str = str.replace(pat, newWord);
// result abc def test abcde xyz

Good luck
:)

Replace last occurrence of character in string

You don't need jQuery, just a regular expression.

This will remove the last underscore:

var str = 'a_b_c';console.log(  str.replace(/_([^_]*)$/, '$1')  ) //a_bc

How to replace the last instance of a character in a string?

You have a _ instead of a , and you've wrapped your regex in quotes. I think you'll also need to add a space before the and:

myString = myString.replace(/,([^,]*)$/,'\ and$1');


Edit:

You could also do this without regex, if you're so inclined:

str = "Maria, David, Charles, Natalie";
lastComma = str.lastIndexOf(',');
newStr = str.substring(0, lastComma) + ' and' + str.substring(lastComma + 1);

//=> "Maria, David, Charles and Natalie"

javascript replace last occurrence of string

newString = oldString.substring(0,oldString.lastIndexOf("_")) + 'aa';

Replace last character of string using JavaScript

You can do it with regex easily,

var str1 = "Notion,Data,Identity,".replace(/.$/,".")

.$ will match any character at the end of a string.

Create a regex to replace the last occurrence of a character in a string

Instead of RegEx, use String#substring and String.lastIndexOf as below

const str = 'Field Name* * ';const replaceStr = 'mandatory';const lastIndex = str.lastIndexOf('*');const result = str.substring(0, lastIndex) + replaceStr + str.substring(lastIndex + 1);
console.log('Substitution result: ', result);

String replace last character occurrence slash

You have to escape the slash character in a regular expression literal. Capture the characters after the last slash until the end of the string and use in the replacement:

s = s.replace(/\/([^\/]*)$/, '$1');

(You don't need the g flag for this one, as you know that there is never more than one match.)

Demo: http://jsfiddle.net/Guffa/jkn52/


Alternatively, use a poositive look-ahead to match a slash that doesn't have another slash until the end of the string:

s = s.replace(/\/(?=[^\/]*$)/, '');

Demo: http://jsfiddle.net/Guffa/jkn52/2/

How to replace last occurrence of characters in a string using javascript

foo.replace(/,([^,]*)$/, ' and $1')

use the $ (end of line) anchor to give you your position, and look for a pattern to the right of the comma index which does not include any further commas.

Edit:

The above works exactly for the requirements defined (though the replacement string is arbitrarily loose) but based on criticism from comments the below better reflects the spirit of the original requirement.