Replace the last occurrence of a character in a string

Is there an easy way in jQuery to replace the last occurrence of "_" (underscore) in a given string?

+60
javascript jquery
Mar 31 '11 at 8:51
source share
10 answers

You do not need jQuery, just a regular expression.

This will remove the last underline:

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

This will replace it with the contents of the replacement variable:

 var str = 'a_b_c', replacement = '!'; str = str.replace(/_([^_]*)$/,replacement+'$1'); //a_b!c 
+113
Mar 31 '11 at 8:55
source share

There is no need for jQuery or regular expressions, assuming that the character you want to replace exists in the string

Replace last character in string

str = str.substring(0,str.length-2)+otherchar

Replace last underline in line

 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) ) 
+62
Mar 31 '11 at 8:52
source share

How about this?

 function replaceLast(x, y, z){ var a = x.split(""); a[x.lastIndexOf(y)] = z; return a.join(""); } replaceLast("Hello world!", "l", "x"); // Hello worxd! 
+6
Jan 27 '17 at 17:40
source share

Cancel the line, replace the char, cancel the line.

Here is a message for changing a string in javascript: How do you change a string in JavaScript?

+2
Feb 19 '13 at 22:36
source share

Keep it simple

 var someString = "a_b_c"; var newCharacter = "+"; var newString = someString.substring(0, someString.lastIndexOf('_')) + newCharacter + someString.substring(someString.lastIndexOf('_')+1); 
+2
Oct. 16 '16 at 15:41
source share

This is very similar to the mplungjan answer, but it can be a little simpler (especially if you need to do other string manipulations immediately after and want to save it as an array) Anyway, I just thought I put it there if someone prefers this is.

 var str = 'a_b_c'; str = str.split(''); //['a','_','b','_','c'] str.splice(str.lastIndexOf('_'),1,'-'); //['a','_','b','-','c'] str = str.join(''); //'a_b-c' 

"_" can be changed with the char you want to replace

And '-' can be replaced with char or the string you want to replace with

+1
Feb 17 '17 at 4:02
source share

Another super clear way to do this could be as follows:

 let modifiedString = originalString .split('').reverse().join('') .replace('_', '') .split('').reverse().join('') 
+1
Feb 21 '19 at 9:30
source share

You can use this code

 var str="test_String_ABC"; var strReplacedWith=" and "; var currentIndex = str.lastIndexOf("_"); str = str.substring(0, currentIndex) + strReplacedWith + str.substring(currentIndex + 1, str.length); alert(str); 
0
Dec 20 '16 at 8:06
source share

  // Define variables let haystack = 'I do not want to replace this, but this' let needle = 'this' let replacement = 'hey it works :)' // Reverse it haystack = Array.from(haystack).reverse().join('') needle = Array.from(needle).reverse().join('') replacement = Array.from(replacement).reverse().join('') // Make the replacement haystack = haystack.replace(needle, replacement) // Reverse it back let results = Array.from(haystack).reverse().join('') console.log(results) // 'I do not want to replace this, but hey it works :)' 
0
May 04 '18 at 19:28
source share

This is a recursive method that removes multiple occurrences of "endchar":

 function TrimEnd(str, endchar) { while (str.endsWith(endchar) && str !== "" && endchar !== "") { str = str.slice(0, -1); } return str; } var res = TrimEnd("Look at me. I'm a string without dots at the end...", "."); console.log(res) 
0
Jun 10 '19 at
source share



All Articles