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