codingtcher.com
Theme Night
Home
Login

Javascript remove last characters from string

//how to find last occurrence comma in a string and replace with value in javascript
var str = 'a_b_c',
    replacement = '!';

console.log(  str.replace(/_([^_]*)$/, replacement + '$1')  ) //a_b!c

//javascript replace last occurrence of a letter
String.prototype.replaceLast = function (search, replace) {
    return this.replace(new RegExp(search+"([^"+search+"]*)$"), replace+"");
}

str = "lala";
newStr = str.replaceLast("l", "m");
console.log(newStr);

//javascript replace last character
var str1 = "Notion,Data,Identity,".replace(/.$/,".")

Related Code