Search code examples
regexvisual-studio-codesublimetext3atom-editor

How can I search and replace backticks with <code> </code> tags?


I have a large text file with multiple instances of text that are enclosed by 3 backticks:

    ```
Default:
Publisher Bid Modifier Object with values=[]
    ```

   <td>

    ```
Publisher Bid Modifier Object
    ```

   </td>

I want replace those backticks with <code> </code> tags, to look like this:

    <code>
Default:
Publisher Bid Modifier Object with values=[]
    </code>

   <td>

    <code>
Publisher Bid Modifier Object
    </code>


   </td>

How could I search and replace using a text editor like VS Code (or similar)? I am using a MacBook.

Many thanks!


Solution

  • Use the regex:

    ```([^`]*)```
    

    and replace it with

    <code>\1</code>
    
    • <code> and </code> are just text strings, can be anything;
    • \1 is the first capture group from the search string; additionally you can have \2, \3 ..., depending on what you search;

    Test here.


    However, make yourself a service and verify that the "tags" are properly matched, either before, or after the replacement. It will be quite tricky to catch an even number of mismatches.