121
rated 0 times
[
122]
[
1]
/ answers: 1 / hits: 133371
/ 13 Years ago, thu, march 31, 2011, 12:00:00
Is there an easy way in javascript to replace the last occurrence of an '_' (underscore) in a given string?
More From » javascript
Is there an easy way in javascript to replace the last occurrence of an '_' (underscore) in a given 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