Replace last occurrence of character in string

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

This will remove the last underscore:

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

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

var str="a_b_c",
    replacement="!";

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

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)