htmlregexvisual-studio-codereplacetags

How to change only certain elements, keeping intact other elements with the search module in VS Code?


I want to know how is it possible to change some HTML tag such as :

<tag1>[my text]</tag1>

to another type of tag such as :

<tag2>[my text]</tag2>

but keeping the inner text intact.

Because it is possible to get all elements with regEx, but how to tell VS code to keep the inner text intact ?

More generally, how to change only certain elements by keep other elements intact in the search module of VS Code ?

Thank you for your help.


Solution

  • Open the search

    CTRL + F to find, then click the down arrow or CTRL + H to open the replacing gui directly.

    How to do it

    Then in the search (first) input enter:

    <tag1>(\[my text\])</tag1>
    

    in the replacement (second) input enter:

    <tag2>$1</tag2>
    

    Explanations

    In the search (first) input:

    (\[my text\]): the () is a capturing group and is the first here.
    The value to capture is [my text] and the scare bracket are escaped \[.
    Maybe you wouldn't have square brackets and instead your regex would be <tag1>(my text)</tag1>.

    In the replacement (second) input:

    The $1 refer to the value of the first capturing group so [my text].

    To go further

    Below is a more practical case. Still using search and replacement regex.

    Search Regex:

    <tag1( +[^>]+)?>((?:.*\r?\n?)*)</tag1>
    

    This regex is used to find a specific tag <tag1> and its content, which will be replaced by another tag.

    Capturing Groups:

    1. $1: Captures any attributes (or spaces) within the opening tag <tag1>. This corresponds to ( +[^>]+)?.
    2. $2: Captures the content inside the opening and closing tags <tag1></tag1>. This corresponds to ((?:.*\r?\n?)*).

    Replacement Regex:

    <tag2$1>$2</tag2>
    

    This replacement regex transforms the matched content:

    Example:

    Original Input:

    <tag1>[my text]</tag1>
    
    <tag1>some other text</tag1>
    
    <tag1></tag1>
    
    <tag1><atag>a tag between</atag></tag1>
    
    <tag1><amtag>
        a tag
        in multilines
        and a
        link <a href="http://www.stackoverflow.com"></a>
    </amtag></tag1>
    
    <tag1>some tags
         in multilines and a class
         <address><data value="sd
            addressdas
            data
            datad"></data></address>
    </tag1>
    
    <tag1 value="2">[my text]</tag1>
    
    <tag1 multi="2"
          lines="3"
          cls="5"
          >[my text]</tag1>
    

    After Replacement:

    <tag2>[my text]</tag2>
    
    <tag2>some other text</tag2>
    
    <tag2></tag2>
    
    <tag2><atag>a tag between</atag></tag2>
    
    <tag2><amtag>
        a tag
        in multilines
        and a
        link <a href="http://www.stackoverflow.com"></a>
    </amtag></tag2>
    
    <tag2>some tags
         in multilines and a class
         <address><data value="sd
            addressdas
            data
            datad"></data></address>
    </tag2>
    
    <tag2 value="2">[my text]</tag2>
    
    <tag2 multi="2"
          lines="3"
          cls="5"
          >[my text]</tag2>