Home > Software engineering >  Change all php variable from snake_case/underscore_case to camelCase using regex and vscode
Change all php variable from snake_case/underscore_case to camelCase using regex and vscode

Time:12-20

I found a neat vscode way to convert snake_case to camelCase but it will affect all words. I want to limit the search to words with $ at the beginnig. With what I searched, I need to add ^$ before the string I want to match but unable to properly work it. Below is the way I found in this github link

1. Press CTRL-H ( ⌥⌘F on Mac ).
2. Press ALT-R ( ⌥⌘R on Mac ).
3. Type _([a-zA-Z]).
4. Press TAB and type $1.
5. Press ALT-ENTER ( ⌥ENTER on Mac ).
6. Press F1 and type upper, then press ENTER.
7. Press CTRL-ALT-ENTER ( ⌥ENTER on Mac ).

meaning change all $is_data to $isData while words like _construct are unchanged.

EDIT To someone that closed my question because he said this is not a question. How to make all PHP variable $snake_case/$underscore_case to $camelCase in VSCODE with REGEX.

CodePudding user response:

You may use this regex:

(?<=\$\w*)_([a-zA-Z])

RegEx Demo

RegEx Details:

  • (?<=\$\w*): Lookbehind to assert that there is a $ followed by 0 or more word characters behind current position
  • _: Match an underscore
  • ([a-zA-Z]): Match a letter and capture in group #1

CodePudding user response:

You can find all words starting with starting $ and min one underscore using this \$\w*_\w* and replace with $1${2:lower}.

  • Related