Skip to main content

replace ()

Replaces all instances of a specified substring from the source string with a given replacement.

Parameters

$string
The source string in which the substring is to be replaced.
Type
String
$substring
The substring to replace in the source string.
Type
String
$replacement

The replacement for each instance of the substring.

A number of special replacement patterns are supported.

$&
Inserts the substring.
$`
Inserts the portion of the source string that precedes the replaced substring.
$'
Inserts the portion of the source string that follows the replaced substring.
Type
String

Return Value

String
A string with all instances of the substring replaced by a replacement.

Example

@use '@sass-fairy/string';

$sentence: 'The quick brown fox jumps over the lazy dog.';

@debug string.replace($sentence, 'jumps', 'skipped');
// 'The quick brown fox skipped over the lazy dog.'

@debug string.replace($sentence, 'jumps', '[$&]');
// 'The quick brown fox [jumps] over the lazy dog.'