skip to Main Content

I have a JSON file in VSCode.

I want to replace all hyphens in the document with underscores but only where the same line contains the text: "product":

For example:

{
    "product": "replace-my-hyphens",
    "label": "do-not-replace-my-hyphens"
}

2

Answers


  1. what to do:

    1. select "product":
    2. menu Selection > Select All Occurrences (Ctrl+Shift+L)
    3. Select till end of line: Shift+End
    4. Open find/replace dialog: Ctrl+F
    5. Select button Find in selection (3 lines)
    6. Fill in (find) - and (replace) _
    7. Click button Replace All
    8. Esc leave multi cursor mode
    Login or Signup to reply.
  2. You can do this with a simple find and replace:

    Find: (?<="product":.*?)-
    Replace: _

    As you can see, any - will be matched that is preceded by "product": and some number of characters (except line terminators).

    See regex101 demo.

    This works in a find within one file (i.e., using the Find Widget) but will not work in a Search across multiple files because of the non-fixed length lookbehind.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search