Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript/jQuery - replace last occurence of a word in a string

I have a string that ends with a fixed word - Button. Now I need to replace that word with ''. How can I do it? Also, if my string is something like ButtonButton or similar, I need to cut out only last Button.

like image 939
ojek Avatar asked Mar 26 '13 14:03

ojek


1 Answers

var original = 'ButtonButton';
var newString = original.replace(/Button$/, '');
// "Button"
like image 172
João Silva Avatar answered Sep 30 '22 17:09

João Silva