Home > Mobile >  Vs code select text between quotes
Vs code select text between quotes

Time:06-27

I have a vast array list like below

$data= [
        'user_name' => 's',
        'user_place' => 'a',
        'address_list_code' => 's',
        'block_number' => 3,
];

so I want to replace the key string with all uppercase.I know to convert selected text to uppercase using vs code shortcut Ctl Alt u and it works. But I want to select only all keys in between a single quote and make it uppercase so the expected output is

[
        'USER_NAME' => 's',
        'USER_PLACE' => 'a',
        'ADDRESS_LIST_CODE' => 's',
        'BLOCK_NUMBER' => 3,
];

Even I tried this extension but not suceded to select all text in between single quotes

https://marketplace.visualstudio.com/items?itemName=dbankier.vscode-quick-select&ssr=false#version-history

CodePudding user response:

Check out this fiddle : https://jsfiddle.net/m82ycfxw/
I made a normal JS code to convert the desired text to upper case.

This might be a temporary thing but I hope this will work for you.

let str = `
$data= [
        'user_name' => 's',
        'user_place' => 'a',
        'address_list_code' => 's',
        'block_number' => 3,
]
`;

var regex = /'[a-z_] ' =>/g;
str = str.replace(regex, foundText => {
  return foundText.toUpperCase();
});
console.log(str);

Just change the str variable. Put all your complete data object inside backticks (` `)and run the code.

CodePudding user response:

The extension Select By could help.

  • Place cursors at the start of the lines where you want to Uppercase text
  • with MoveBy: Move cursors based on regex move to the next '
  • with SelectBy: Mark positions of cursors
  • with MoveBy: Move cursors based on regex move to the next '
  • with SelectBy: Mark positions of cursors (create selections)
  • execute: Transform to Uppercase
  • Esc to exit multi Cursor

CodePudding user response:

Sure, you can do this natively:

  1. In settings.json, temporarily remove _ as a word separators (e.g. "editor.wordSeparators": "!@#$%^&*()-= [{]}\\|;:'\",.<>/?,) (Notice no _) .
  2. Place cursors at beginnings of all desired lines.
  3. Right arrow to move all cursors within the quotes.
  4. Execute command expand selection. Since you turned off _ as a word delimiter, this will expand to fill the quotes; otherwise, all the keys would need to have the same number of words for this to work.
  5. Execute upper case.
  6. In settings.json re-add _ to word separators.

CodePudding user response:

Easy with Find and Replace. See regex101 demo

Find: (^\s ')([^']*)'
Replace: $1\U$2'

The \U will uppercase the following capture group $2.

Starting the find at the beginning of the line with ^ makes it easy to target just the "keys" (the first '-delimmited strings) and not the other following strings.

  • Related