Well, if the string really ends with the pattern, you could do this:
str = str.replace(new RegExp(list[i] + '$'), 'finish');
Answer from Pointy on Stack OverflowWell, if the string really ends with the pattern, you could do this:
str = str.replace(new RegExp(list[i] + '$'), 'finish');
You can use String#lastIndexOf to find the last occurrence of the word, and then String#substring and concatenation to build the replacement string.
n = str.lastIndexOf(list[i]);
if (n >= 0 && n + list[i].length >= str.length) {
str = str.substring(0, n) + "finish";
}
...or along those lines.
regex - Replace last occurrence word in javascript - Stack Overflow
regex - javascript replace last occurrence of string - Stack Overflow
regex - How to replace last occurrence of characters in a string using javascript - Stack Overflow
Javascript string replace last ocurrence - Stack Overflow
You can use a regular expression.
This will remove the last underscore:
var str = 'a_b_c';
console.log( str.replace(/_([^_]*)$/, '$1') ) //a_bc
Run code snippetEdit code snippet Hide Results Copy to answer Expand
This will replace it with the contents of the variable replacement:
var str = 'a_b_c',
replacement = '!';
console.log( str.replace(/_([^_]*)$/, replacement + '$1') ) //a_b!c
Run code snippetEdit code snippet Hide Results Copy to answer Expand
No need for jQuery nor regex assuming the character you want to replace exists in the string
Replace last char in a string
str = str.substring(0,str.length-2)+otherchar
Replace last underscore in a string
var pos = str.lastIndexOf('_');
str = str.substring(0,pos) + otherchar + str.substring(pos+1)
or use one of the regular expressions from the other answers
var str1 = "Replace the full stop with a questionmark."
var str2 = "Replace last _ with another char other than the underscore _ near the end"
// Replace last char in a string
console.log(
str1.substring(0,str1.length-2)+"?"
)
// alternative syntax
console.log(
str1.slice(0,-1)+"?"
)
// Replace last underscore in a string
var pos = str2.lastIndexOf('_'), otherchar = "|";
console.log(
str2.substring(0,pos) + otherchar + str2.substring(pos+1)
)
// alternative syntax
console.log(
str2.slice(0,pos) + otherchar + str2.slice(pos+1)
)
Run code snippetEdit code snippet Hide Results Copy to answer Expand
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 :)
// create array
var words = $(element).html().split(" ");
// find last word and replace it
words[words.lastIndexOf("abc")] = newWord
// put it back together
words = words.join(" ");
You can use this replace:
var str = '12-44-12-1564';
str = str.replace(/12(?![\s\S]*12)/, 'aa');
console.log(str);
explanations:
(?! # open a negative lookahead (means not followed by)
[\s\S]* # all characters including newlines (space+not space)
# zero or more times
12
) # close the lookahead
In other words the pattern means: 12 not followed by another 12 until the end of the string.
newString = oldString.substring(0,oldString.lastIndexOf("_")) + 'aa';
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.
console.log(
'test1, test2, test3'.replace(/,\s([^,]+)
1')
)
result = dialog.replace(/,\s(\w+)$/, " and $1");
$1 is referring to the first capturing group (\w+) of the match.
Why last 'AND' is not removed/replaced?
Because you are trying to replace the output of lastIndexOf which is an
integer
Use replace with regex
obj.where_str = obj.where_str.replace( /(AND)\s*$/, "" )
/(AND)\s*$/ will replace AND near the end of the string may (or may not) be followed by spaces.
You can do this:
var obj = {};
obj.where_str ="CD_MAQ LIKE '%AA%' AND";
var lastIndex = obj.where_str.lastIndexOf(" ");
obj.where_str = $.trim(obj.where_str.substring(0, lastIndex));
console.log(obj.where_str);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>