Replace the substring before the last occurrence of a specified search string.
var replaceBeforeLast = require( '@stdlib/string/base/replace-before-last' );
Replaces the substring before the last occurrence of a specified search string.
var str = 'beep boop';
var out = replaceBeforeLast( str, ' ', 'loop', str.length );
// returns 'loop boop'
out = replaceBeforeLast( str, 'o', 'bar', str.length );
// returns 'barop'
- If a search string is not present in a provided string, the function returns the provided string unchanged.
- If a search string is an empty string, the function returns the provided string unchanged.
- If
fromIndex
is less than0
or greater than thesearch
string length, the function returns the provided string unchanged.
var replaceBeforeLast = require( '@stdlib/string/base/replace-before-last' );
var str = 'beep boop';
var out = replaceBeforeLast( str, 'p', 'see', str.length );
// returns 'seep'
str = 'Hello World!';
out = replaceBeforeLast( str, 'xyz', 'foo', str.length );
// returns 'Hello World!'
str = 'Hello World!';
out = replaceBeforeLast( str, '', 'foo', str.length );
// returns 'Hello World!'
str = '';
out = replaceBeforeLast( str, 'xyz', 'foo', str.length );
// returns ''