string.replace-multiple()

The string.replace-multiple() function handles multiple string replaces, using string.replace() function.

// Modules.
@use 'string.replace.function';

// The `string.replace-multiple()` function.
@function replace-multiple($string, $replaces...) {
  @each $occurrence, $substring, $replacement in $replaces {
    $string: string.replace($string, $occurrence, $substring, $replacement);
  }
  @return $string;
}

Parameters

$string

The string to do multiple $replaces.

$replaces...

Arbitrary replaces (occurrence, substring, replacement) to do on $string.

Return

The return value is a string with done $replaces.

Examples

// Use.
@use '@angular-package/sass/string';

// Examples.
// single replacement first occurrence
@debug string.replace-multiple('bold king is hairy', first 'bold' 'baloon'); // baloon king is hairy
@debug string.replace-multiple('bold king is hairy', first 'bold' ''); //  king is  hairy
@debug string.replace-multiple(':==', first ':' '');

// single replacement all occurrences
@debug string.replace-multiple('bold king is bold hairy', all 'bold' ''); //  king is  hairy
@debug string.replace-multiple('bold king is bold hairy', all 'bold' 'test'); // test king is test hairy

// multiple replacements
@debug string.replace-multiple('bold king is hairy', first (bold is) ''); //  king  hairy
@debug string.replace-multiple('bold king is hairy', first bold baloon, first king baloon); // baloon baloon is hairy
@debug string.replace-multiple('bold king is hairy', first (bold king) baloon, first (is hairy) bold); // baloon baloon bold bold
@debug string.replace-multiple('atmosphere bold bold bold king is hairy', first (king bold) baloon); // atmosphere baloon bold bold baloon is hairy
@debug string.replace-multiple('atmosphere bold bold bold king is hairy', all (king bold) baloon); // atmosphere baloon baloon baloon baloon is hairy

// multiple replacements different occurrences
@debug string.replace-multiple('atmosphere bold bold bold king is hairy', first bold clean, all (king bold) baloon); // atmosphere clean baloon baloon baloon is hairy

Last updated