2

Hi i want to target all the scss variables which is used for styles

Question: i want to target all the scss variables which comes after : with $name it might come inside ($name) as shown in below image (red color highlight)

My Expectation is shown in below image(red color highlight):

enter image description here

From above image i want to highlight all the red color marked variables

here is what i have tried

(:)(\s+\$+.*;)

Which fails so many test cases

Demo: https://regex101.com/r/WtyoF6/1

Request: please include any other testcases if you know.

Please help me thanks in advance !!!!

EaBengaluru
  • 131
  • 2
  • 17
  • 59

5 Answers5

2

There is no tool or language listed, but for the example data, if a quantifier in a lookbehind assertion is supported:

(?<=:[^:$\n]*)\$\w+(?:-\w+)*

Explanation

  • (?<= Positive lookbehind, assert what is to the left is
    • :[^:$\n]* Match : followed by optionally repeating any char except : $ or a newline
  • ) Close the lookbehind assertion
  • \$\w+(?:-\w+)* Match $ 1+ word chars and optionally repeat matching - and 1+ word chars

Regex demo

Or else with a capture group:

(?::[^:$\n]*)(\$\w+(?:-\w+)*)

Regex demo

The fourth bird
  • 154,723
  • 16
  • 55
  • 70
1

If you are looking for all occurrences of SCSS variables that are being used, this will work for you:

:.*(\$[a-zA-Z-]+)

A demo can be found here.

Aaron Meese
  • 1,670
  • 3
  • 22
  • 32
1

Using this answer as a reference for the valid SASS/SCSS variable characters the following regex should match all* valid SASS/SCSS variables:

(\$(?![0-9])(?:[a-zA-Z0-9-_\u0080-\uD7FF\uE000-\uFFFD]|(?:\\[!"#$%&'\(\)*+,.\/:;<=>?@\[\]^{|}~]))+)

Explaination

  • ( capturing group around the whole SASS variable name
    • \$ starts with a $
    • (?![0-9]) negative lookahead - the first character cannot be a number
    • (?: non-capturing group of the valid characters
      • [a-zA-Z0-9-_\u0080-\uD7FF\uE000-\uFFFD] any letter, number, dash, underscore, or characters with the ranges 0080 to D7FF, E000 to FFFD (the range 10000 to 10FFFF is missing because the regex engine won't accept it as a valid range)
      • | or
      • (?: non-capturing group of special characters that are escaped buy a \
        • \\ back-slash
        • [!"#$%&'\(\)*+,.\/:;<=>?@\[\]^{|}~] any of these special characters
      • )
    • )+ one or more of the valid characters
  • )

* Any characters in the range 10000 to 10FFFF won't match as the regex engine won't accept this as a valid range.

Demo

Jimbojim
  • 311
  • 3
  • 6
1

This regex will exactly find all the red highlighted values in your example (see this demo):

(?<=:[^$\n]*)(\$[^\s:;,]*)

Or if you prefer to avoid the positive lookbehind, use the first capturing group from here (see this demo):

(?::[^$\n]*)(\$[^\s:;,]*)
Steve Chambers
  • 37,270
  • 24
  • 156
  • 208
0

(:)(.+)(\$.+((,)))|(:)(.+)(\$.+((\s)))|(:)(.+)(\$.+(([;])))

In my hack above, I match everything that comes after : until space\s, comma,, or semicolon; and then differentiate the appropriate $name in the $3 of $1$2$3$4.

So, if you need to replace the $name you will use $1$2$3$4 and change the $3 to what you need to replace it with.

Demo

Phyln
  • 69
  • 7