Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String replace in Sass

Tags:

Sass 3.2.1

How to replace in string some part of text? For example, remove from color #fefefe a dash #, to be fefefe.

Thanks!

like image 244
Alex Ivasyuv Avatar asked Oct 04 '12 13:10

Alex Ivasyuv


People also ask

How to replace a substring with another string in Sass?

Sass provides a collection of handy functions to manipulate strings, however there is no function to replace a substring with another string. Here is a quick str-replace function if you ever need one. .selector { content: "Thxoxo answxoxor to lifxoxo thxoxo univxoxorsxoxo and xoxovxoxorything is 42.";

What is string function in Sass?

Sass String Functions The string functions are used to manipulate and get information about strings. Sass strings are 1-based. The first character in a string is at index 1, not 0.

How do you replace a character in a string in Excel?

Start the TRANWRD function. The TRANWRD function is a versatile function to replace one value with another. Specify the input variable that contains the character you want to replace. This input variable can be a column name, a string, or an expression. Specify the character that you want to replace.

Why is there extra space between a and day in Sass?

Notice the extra space in the CSS (between a and day), which comes from the null value being converted to an empty string. While Sass provides only the single operator for strings, it does have a few built-in functions for manipulating strings. They might not appear useful at first, but they can be very useful when writing mixins and functions.


1 Answers

Here is a function that I just used!

SCSS-FUNCTION

/// 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;
}

SCSS-USAGE:

.selector {
  $string: 'The answer to life the universe and everything is 42.';
  content: str-replace($string, 'e', 'xoxo');
}

SCSS-RESULT:

.selector {
  content: "Thxoxo answxoxor to lifxoxo thxoxo univxoxorsxoxo and xoxovxoxorything is 42.";
}

Source of SCSS-example

I use SASS with indented style, so this is my conversion to get a color variable to replace in a background with data:href inline encoded css svg-image. The # needs to be url-encoded and I use global colors that only needs to be replaced in one place and simply just work!

SASS-FUNCTION:

@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

SASS-USAGE:

$color-blue-night: #172b47    
$icon-normal: str-replace('' + $color-blue-night, '#', '')

or for a clear example

   .test
     color: str-replace('' + $color-blue-night, '#', '')

SASS-RESULT:

.test {
  color: "172b47"; }
like image 167
Valross.nu Avatar answered Sep 28 '22 11:09

Valross.nu