kName = "cglsamaz";
cName = kName.replace("a","e");
document.getElementById("anythingx").innerHTML = cName;
this codes result : cglsemaz
what i want : cglsamez
kName = "cglsamaz";
cName = kName.replace("a","e");
document.getElementById("anythingx").innerHTML = cName;
this codes result : cglsemaz
what i want : cglsamez
I'm assuming you want a bit of a generic solution not just last a
literally, otherwise a Regex pattern could do it.
For that can use a combination of lastIndexOf
and substring
:
var kName = "cglsamaz";
var valueToReplace = 'a';
var newValue = 'e';
var replaceIndex = kName.lastIndexOf(valueToReplace);
var cName =
kName.substring(0, replaceIndex) +
newValue +
kName.substring(replaceIndex + valueToReplace.length);
document.getElementById("anythingx").innerHTML = cName;
<div id="anythingx"></div>
Here is a generalized solution that uses a function as the second parameter to the .replace(...)
method:
function replacerMaker(rplc, inst) {
// This is to keep track of the numbered instance found
var idx = 0;
// this returns a function which will replace the numbered instance of
// the search string with the replacement string
return function(match, offset, string) {
if (++idx === inst) {
return rplc;
}
return match;
}
}
// A slightly longer name to test instances 3, 4, etc.
var kName = "cglsamazabkdsakuodakdif";
var cName = kName.replace(/a/g, replacerMaker("e", 2));
document.getElementById("anythingx").innerHTML = "Second Instance: " + cName;
cName = kName.replace(/a/g, replacerMaker("e", 4));
document.getElementById("anythingx").innerHTML += "<br />" + "Fourth Instance: " + cName;
<div id="anythingx"></div>
There is a smart alternative to my previous answer:
kName = "cglsamaz";
cName = kName.replace("a","*TEMP*");
cName = cName.replace("a","e");
cName = cName.replace("*TEMP*","a");
document.getElementById("anythingx").innerHTML = cName;
Replaces first occurrence with a temporary value, allowing to replace the second occurrence (now it is the first) as needed and then restores the original value to the temporary one! Can be simplified in a single string.
(I created a new answer because it is a totally different approach)
You can reverse it and then replace the a with and e and then reverse it again.
kName = "cglsamaz";
kName = kName.split("").reverse().join("")
cName = kName.replace("a","e");
cName = cName.split("").reverse().join("")
However this seems quite long winded for a simple change Hope that helps
kName = "cglsamaz";
cName = kName.replace(/a(?=[^a]+$)/i,"e");
document.getElementById("anythingx").innerHTML = cName;
This will replace only the last “a” in any string.
kName = "cglsamaz";
cName = kName.split("").reverse().join("").replace(/a(?=[^a]{0,}a{1,1}[^a]{0,}$)/i,"e").split("").reverse().join("");
document.getElementById("anythingx").innerHTML = cName;
Unfortunately, JavaScript does not support Positive LookBehind. This code will reverse the string, look for the second “a” from the end (which is the second “a” from the start of the original string), replace it and then reverse it again.
This does exactly what asked, and will work for any number of “a” characters, case-insensitive. To make it work with other characters too, RegExp could be modified dynamically.