Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to replace values that include part of match in replacement in sublime?

I've come up with this regex that finds all words that start with $ and contain _ underscores:

\$(\w+)_(\w+)

I'm basically searching for variables, like $var_foo etc.

How do I replace stuff using the regex groups?

For example, how can I remove the underscore and make the next letter uppercase, like $varFoo ?

like image 920
Alex Avatar asked May 31 '12 20:05

Alex


People also ask

Can I use regex in replace?

How to use RegEx with . replace in JavaScript. To use RegEx, the first argument of replace will be replaced with regex syntax, for example /regex/ . This syntax serves as a pattern where any parts of the string that match it will be replaced with the new substring.

How do I use regular expressions in Sublime Text?

To use regular expressions in Sublime Text, first activate them in the corresponding search panel by clicking on the available buttons or using keyboard shortcuts. If you don't activate regular expressions before performing a search, the search terms will be interpreted literally. Documentation on regular expressions.

What is $1 in regex replace?

For example, the replacement pattern $1 indicates that the matched substring is to be replaced by the first captured group.


1 Answers

The replacement expression is:

\$\1\u\2 
  • \1, \2 are the captures (or $1, $2)
  • \u up-cases (see the Replacement String Syntax section).

See the Regular Expressions chapter (in the TextMate docs) for more information.

There's already a package that does this, and more:

  • Brief blog about CaseConversion
  • CaseConversion package
like image 99
Dave Newton Avatar answered Sep 20 '22 05:09

Dave Newton