Replace last word in javascript

I have a problem replacing the last word in JS, I am still looking for a solution, but I cannot get it.

I have this code:

var string = $(element).html(); // "abc def abc xyz" var word = "abc"; var newWord = "test"; var newV = string.replace(new RegExp(word,'m'), newWord); 

I want to replace the last word "abc" in this line, but now I can replace all or the first occurrence in the line. How can i do this? Maybe this is not a good way?

+3
javascript regex replace
Apr 17 '14 at 15:01
source share
7 answers

Here is the idea ....

This is a lowercase search case-sensitive 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 need a case-insensitive version, then the code must be changed. Let me know and I can change it for you. (PS. I am doing this, so I will publish it soon)

Update: The following is an example of a lowercase case-sensitive 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 

NB The above codes are looking for a string. not a whole word (i.e. with word boundaries in RegEx). If a string should be a whole word, it should be recycled.

Update 2:. This is a case-insensitive full 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 :)

+10
Apr 17 '14 at 15:20
source share
 // 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(" "); 
+5
Apr 17 '14 at 15:05
source share

You can use lookahead to get the last word in a sentence:

 var string = "abc def abc xyz"; var repl = string.replace(/\babc\b(?!.*?\babc\b)/, "test"); //=> "abc def test xyz" 
+3
Apr 17 '14 at 15:05
source share

You want both:

  • matching abc
  • make sure there is no line after abc line

So you can use:

 abc(?!.*abc) 

(?!...) is a negative look, it will not be able to combine all the regular expression if what is inside the lookahead is consistent.

Also be careful, as this will match abc in abcdaire : if you want only abc as a separate word, you need to add \b word boundaries:

 \babc\b(?!.*\babc\b) 
+3
Apr 17 '14 at 15:05
source share

try

 var string = $(element).html(); // "abc def abc xyz" var word = "abc"; var newWord = "test"; var newV = string.replace(new RegExp(word+'$'), newWord); 
+1
Apr 17 '14 at 15:22
source share

I'm not very familiar with JavaScript, but you can probably flip this to fit your needs:

(\b\w+\b)(.*)(\1) replace with \1\2+'your_key_word'

Watch the demo to see what I mean.

0
Apr 17 '14 at 15:05
source share

You can use the fact that the replace method replaces only the first occurrence of the target string, if used without a global flag, and try something like:

"abc def abc xyz abc jkl".split(' ').reverse().join(' ').replace('abc', 'test').split(' ').reverse().join(' ')

0
Apr 17 '14 at 16:12
source share



All Articles