I spent some time recently converting some SCSS code to Stylus, and one of my regex steps was to strip out the $
character in variable names, while formatting those names to camelCase rather than hyphenated-case.
For example, $long-hyphenated-variable-name: value;
became longHyphenatedVariableName = value;
but parameter names like text-size
did not become textSize
.
anyway, my Regex skills are pretty limited, so I could only figure out how to do it as a series of passes, looking first for variable names that might have as much as 6 parts (5 hyphens) and then for shorter ones on subsequent passes and passing each part of the string as a separate backreference so I could capitalize the first letter. But I wondered if there was a better way to do it: i.e. is there a way to get a variable number of sub-string matches within the string and supply back-references in accordance with how many you got? I just couldn't find any information on this.
This is my code, in six steps, which I'd like to condense to one if it's possible (I'm thinking of writing a multi-step regex routine that can convert SCSS to Stylus for future use). In each case the search expression is followed by the replacement expression:
\$(\S*?)\-(\S*?)\-(\S*?)\-(\S*?)\-(\S*?)\-(.)
$1\u$2\u$3\u$4\u$5\u$6
\$(\S*?)\-(\S*?)\-(\S*?)\-(\S*?)\-(.)
$1\u$2\u$3\u$4\u$5
\$(\S*?)\-(\S*?)\-(\S*?)\-(.)
$1\u$2\u$3\u$4
\$(\S*?)\-(\S*?)\-(.)
$1\u$2\u$3
\$(\S*?)\-(.)
$1\u$2
\$(.)
$1