Replace last occurence of character in string

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

Replace every character occurence but last

Lets say I have a string of a.b.c.d. How do I write a method which will transform that string into abc.d ? Or is there any method available implementation out there ? What I have tried so far int dot

replace string after each second occurence of character

Working with a javascript-variable, I want to replace each occurence of a word (in the example: ‘OR’ with the ||-sign), but only if it does NOT occur in a doublequoted string. Is it possible using a r

PHP Replace Last Occurence of a String in a String?

Anyone know of a very fast way to replace the last occurrence of a string with another string in a string?

Regex to display a string from the second last occurence of a character

Need an Regex to display a string from the second last occurence of a character Example: /folder1/folder2/folder3/folder4/ In this case, if I ask for 2nd occurrence of slash (/) from last , it appea

How to replace the last occurence of the character in .net

i want to replace the last occurence of <li> to add class inside it, it should look like below. <li class=last> <li><span>Vislink Acquires Gigawave for US$6 Million </span

How to replace the last occurence of an expression in a string

Is there a quick way in Python to replace strings by starting from the end? For example: >>> def rreplace(old, new, occurence) >>> … # Code to replace the last occurences of old by

Replace the first and last occurence of a word IF

With this peace of code i replace the first and the last occurence of a word in string. $text = ‘A United States aircraft fly in United States in the United States area.’; $find = ‘United States’; $r

Replace the last occurence of a character in a file

I have this AWK script which replaces every form feed character (0x0C, 12) with 5 new line characters: f=${*:–} awk ‘BEGIN {FF=sprintf(%c,12); LF=sprintf(/n/n/n/n/n)} {i1=0;for(i2=i1+1;i2<=l

Split String by last occurence

I have a String like this: www.myserver.net/Files/Pictures/2014/MyImage.jpg And I want to split it, so I get the Substring after the last occurence of /. Which means I like to get MyImage.jpg I trie

Replace last occurrence of character in string

I’ve got the following string : 01/01/2014 blbalbalbalba blabla/blabla I would like to replace the last slash with a space, and keep the first 2 slashes in the date. The closest thing I have come up

Answers

No need for jQuery nor regex

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

to replace last char in a string

var pos = str.lastIndexOf('_');
str = str.substring(0,pos) + otherchar + str.substring(pos+1)

to replace last underscore

or use one of the regular expressions you will get in a minute from someone šŸ™‚

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

This will remove the last underscore:

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

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

var str = 'a_b_c', replacement = '!';
str = str.replace(/_([^_]*)$/,replacement+'$1'); //a_b!c

Reverse the string, replace the char, reverse the string.

Here is a post for reversing a string in javascript: How do you reverse a string in place in JavaScript?

Keep it simple

var someString = "a_b_c";
var newCharacter = "+";

var newString = someString.substring(0, someString.lastIndexOf('_')) + newCharacter + someString.substring(someString.lastIndexOf('_')+1);

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);

What 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!

This is very similar to mplungjan’s answer, but can be a bit easier (especially if you need to do other string manipulation right after and want to keep it as an array) Anyway, I just thought I’d put it out there in case someone prefers it.

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'

The ‘_’ can be swapped out with the char you want to replace

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