Skip to content

Instantly share code, notes, and snippets.

@delphinpro
Created April 2, 2017 09:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save delphinpro/6e048196bbdfdfee07b1fb5c9ea4caef to your computer and use it in GitHub Desktop.
Save delphinpro/6e048196bbdfdfee07b1fb5c9ea4caef to your computer and use it in GitHub Desktop.
String replace function for SASS
/// Replace `$search` with `$replace` in `$string`
/// @author Hugo Giraudel
/// @param {String} $string - Initial string
/// @param {String} $search - Substring to replace
/// @param {String} $replace ('') - New value
/// @return {String} - Updated string
@function str-replace($string, $search, $replace: '') {
$index: str_index($string, $search);
@if $index {
@return str_slice($string, 1, $index - 1) + $replace + str-replace(str-slice($string, $index + str-length($search)), $search, $replace);
}
@return $string;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment