|
| 1 | +# Custom Regex Commands |
| 2 | + |
| 3 | +The String Manipulation extension now supports custom regex-based commands that you can configure yourself! |
| 4 | + |
| 5 | +## How to Access Custom Commands |
| 6 | + |
| 7 | +1. Select some text in your editor |
| 8 | +2. Open Command Palette (`Cmd+Shift+P` on Mac, `Ctrl+Shift+P` on Windows/Linux) |
| 9 | +3. Type "String Manipulation: Custom Command" |
| 10 | +4. Choose from your configured custom commands |
| 11 | + |
| 12 | +## How to Configure Custom Commands |
| 13 | + |
| 14 | +### Via Settings UI |
| 15 | +1. Open VS Code Settings (`Cmd+,` or `Ctrl+,`) |
| 16 | +2. Search for "string manipulation custom" |
| 17 | +3. Click "Edit in settings.json" next to "Custom Commands" |
| 18 | + |
| 19 | +### Via settings.json |
| 20 | +Add this to your VS Code settings: |
| 21 | + |
| 22 | +```json |
| 23 | +{ |
| 24 | + "stringManipulation.customCommands": [ |
| 25 | + { |
| 26 | + "name": "Extract Numbers", |
| 27 | + "searchPattern": "[^0-9]", |
| 28 | + "replacement": "", |
| 29 | + "global": true |
| 30 | + }, |
| 31 | + { |
| 32 | + "name": "Wrap in Quotes", |
| 33 | + "searchPattern": "^(.*)$", |
| 34 | + "replacement": "\"$1\"" |
| 35 | + }, |
| 36 | + { |
| 37 | + "name": "Camel to Snake", |
| 38 | + "searchPattern": "([a-z])([A-Z])", |
| 39 | + "replacement": "$1_$2", |
| 40 | + "global": true |
| 41 | + }, |
| 42 | + { |
| 43 | + "name": "Remove Vowels", |
| 44 | + "searchPattern": "[aeiouAEIOU]", |
| 45 | + "replacement": "", |
| 46 | + "global": true |
| 47 | + }, |
| 48 | + { |
| 49 | + "name": "Add Prefix", |
| 50 | + "searchPattern": "^(.*)$", |
| 51 | + "replacement": "PREFIX_$1", |
| 52 | + "global": true, |
| 53 | + "multiline": true |
| 54 | + }, |
| 55 | + { |
| 56 | + "name": "Case Insensitive Replace", |
| 57 | + "searchPattern": "hello", |
| 58 | + "replacement": "hi", |
| 59 | + "global": true, |
| 60 | + "ignoreCase": true |
| 61 | + } |
| 62 | + ] |
| 63 | +} |
| 64 | +``` |
| 65 | + |
| 66 | +## Regex Features Supported |
| 67 | + |
| 68 | +- **Capture Groups**: Use `$1`, `$2`, etc. to reference captured groups |
| 69 | +- **Full Match**: Use `$&` to reference the entire match |
| 70 | +- **Named Flag Properties**: Use boolean properties for clear flag configuration |
| 71 | +- **All JavaScript regex features**: Full JavaScript regex syntax support |
| 72 | + |
| 73 | +## Regular Expression Flags |
| 74 | + |
| 75 | +Instead of using cryptic flag letters, use clear boolean properties in your custom commands. These correspond to the [JavaScript RegExp flags](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions#advanced_searching_with_flags): |
| 76 | + |
| 77 | +| Property | Flag | Description | Default | |
| 78 | +|----------|------|-------------|---------| |
| 79 | +| `global` | `g` | Find all matches rather than stopping after the first match | `true` | |
| 80 | +| `ignoreCase` | `i` | Case-insensitive search | `false` | |
| 81 | +| `multiline` | `m` | `^` and `$` match start/end of line, not just start/end of string | `false` | |
| 82 | +| `dotAll` | `s` | `.` matches newline characters | `false` | |
| 83 | +| `unicode` | `u` | Enable full Unicode support | `false` | |
| 84 | +| `sticky` | `y` | Match only at the index indicated by the lastIndex property | `false` | |
| 85 | + |
| 86 | +**Example with flags:** |
| 87 | +```json |
| 88 | +{ |
| 89 | + "name": "Case Insensitive Word Replace", |
| 90 | + "searchPattern": "\\bhello\\b", |
| 91 | + "replacement": "hi", |
| 92 | + "global": true, |
| 93 | + "ignoreCase": true, |
| 94 | + "multiline": true |
| 95 | +} |
| 96 | +``` |
| 97 | + |
| 98 | +## Examples |
| 99 | + |
| 100 | +### Extract Only Numbers |
| 101 | +```json |
| 102 | +{ |
| 103 | + "name": "Extract Numbers", |
| 104 | + "searchPattern": "[^0-9]", |
| 105 | + "replacement": "", |
| 106 | + "global": true |
| 107 | +} |
| 108 | +``` |
| 109 | +Input: `abc123def456` → Output: `123456` |
| 110 | + |
| 111 | +### Convert camelCase to snake_case |
| 112 | +```json |
| 113 | +{ |
| 114 | + "name": "Camel to Snake", |
| 115 | + "searchPattern": "([a-z])([A-Z])", |
| 116 | + "replacement": "$1_$2", |
| 117 | + "global": true |
| 118 | +} |
| 119 | +``` |
| 120 | +Input: `camelCaseString` → Output: `camel_Case_String` |
| 121 | + |
| 122 | +### Wrap Lines in Quotes |
| 123 | +```json |
| 124 | +{ |
| 125 | + "name": "Wrap in Quotes", |
| 126 | + "searchPattern": "^(.*)$", |
| 127 | + "replacement": "\"$1\"", |
| 128 | + "global": true, |
| 129 | + "multiline": true |
| 130 | +} |
| 131 | +``` |
| 132 | +Input: `hello world` → Output: `"hello world"` |
| 133 | + |
| 134 | +### Extract Email Domains |
| 135 | +```json |
| 136 | +{ |
| 137 | + "name": "Extract Email Domain", |
| 138 | + "searchPattern": ".*@(.+)", |
| 139 | + "replacement": "$1", |
| 140 | + "global": true |
| 141 | +} |
| 142 | +``` |
| 143 | +Input: `[email protected]` → Output: `example.com` |
| 144 | + |
| 145 | +### Case-Insensitive Replace |
| 146 | +```json |
| 147 | +{ |
| 148 | + "name": "Replace Hello (Any Case)", |
| 149 | + "searchPattern": "hello", |
| 150 | + "replacement": "hi", |
| 151 | + "global": true, |
| 152 | + "ignoreCase": true |
| 153 | +} |
| 154 | +``` |
| 155 | +Input: `Hello HELLO hello` → Output: `hi hi hi` |
| 156 | + |
| 157 | +## Validation |
| 158 | + |
| 159 | +The extension validates your regex patterns and will show helpful error messages if: |
| 160 | +- The regex pattern is invalid |
| 161 | +- Command names contain invalid characters |
| 162 | +- Required fields are missing |
| 163 | + |
| 164 | +## Features |
| 165 | + |
| 166 | +- **Live Preview**: See transformation previews in the command picker |
| 167 | +- **Multi-selection Support**: Works with multiple text selections |
| 168 | +- **Dynamic Updates**: Changes to settings immediately update available commands |
| 169 | +- **Integration**: Works with the repeat last action feature |
| 170 | +- **Error Handling**: Clear error messages for invalid configurations |
| 171 | + |
| 172 | +## Differences from VS Code Find & Replace |
| 173 | + |
| 174 | +- **Saved Commands**: Your regex transformations are saved and reusable |
| 175 | +- **Named Commands**: Give your transformations meaningful names |
| 176 | +- **Quick Access**: Access via Command Palette without opening find/replace dialog |
| 177 | +- **Multi-selection**: Apply to multiple selections at once |
| 178 | +- **Integration**: Works seamlessly with other String Manipulation features |
0 commit comments