const regex = /(?<=:[^:$\n]*)\$\w+(?:-\w+)*/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('(?<=:[^:$\\n]*)\\$\\w+(?:-\\w+)*', 'gm')
const str = `.dabur-india{
color: \$color-brand-group-3-light !important;
.mmm{
background: \$color-brand-group-2-dark;
\$border-dark: rgba(\$base-color, 0.88);
.alert {
border: 1px solid \$border-dark;
}
}
}
\$black: #000 !default;
\$border-radius: 0.25rem !default;
\$box-shadow: 0 0.5rem 1rem rgba(\$black, 0.15) !default;
code {
border-radius: \$border-radius;
box-shadow: \$box-shadow;
}
@if \$dark-theme {
\$primary-color: darken(\$primary-color, 60%);
\$accent-color: lighten(\$accent-color, 60%);
}
\$theme-colors: (
"success": #28a745,
"info": #17a2b8,
"warning": #ffc107,
);
.alert {
// Instead of \$theme-color-#{warning}
background-color: map.get(\$theme-colors, "warning");
}`;
// Reset `lastIndex` if this regex is defined globally
// regex.lastIndex = 0;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for JavaScript, please visit: https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions