Replace the substring after the first occurrence of a specified search string.
var replaceAfter = require( '@stdlib/string/base/replace-after' );
Replaces the substring after the first occurrence of a specified search string.
var str = 'beep boop';
var replacement = 'foo';
var out = replaceAfter( str, replacement, 'o' );
// returns 'beep bofoo'
out = replaceAfter( str, replacement, ' ' );
// returns 'beep foo'
By default, the search starts at the beginning of the string. To start searching from a different index, provide a fromIndex
argument:
var str = 'boop baz boop';
var replacement = 'foo';
var out = replaceAfter( str, replacement, 'o', 3 );
// returns 'boop baz bofoo'
- If a substring is not present in a provided string, the function returns an empty string.
- If provided an empty substring, the function returns the input string.
- If
fromIndex
is less than0
or greater thanstr.length
, the search starts at index0
andstr.length
, respectively.
var replaceAfter = require( '@stdlib/string/base/replace-after' );
var str = 'To be, or not to be, that is the question.';
var replacement = 'foo';
var out = replaceAfter( str, replacement, ', ' );
// returns 'To be, foo'
out = replaceAfter( str, replacement, 'to be' );
// returns 'To be, or not to befoo'
out = replaceAfter( str, replacement, 'question.' );
// returns 'To be, or not to be, that is the question.foo'
out = replaceAfter( str, replacement, 'xyz' );
// returns 'To be, or not to be, that is the question.'
out = replaceAfter( str, replacement, '' );
// returns 'foo'