Community
    • Login

    Regex: Replace tag with another tag from text

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    6 Posts 3 Posters 1.0k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Robin CruiseR
      Robin Cruise
      last edited by

      hello and happy new year. Maybe you can help me. I need to replace a tag with another tag.

      <p><span class="my_home">My text here.</span></p>

      should become:

      <p class="my_home">My text here.</p>

      Can anyone help me?

      1 Reply Last reply Reply Quote 0
      • PeterJonesP
        PeterJones
        last edited by

        @Robin-Cruise said in Regex: Replace tag with another tag from text:

        Can anyone help me?

        Thank you before and after. I have expanded the example mildly:

        <p><span class="my_home">My text here.</span></p>
        <p><span class="other_class">Other text
        there.</span></p>
        <p>No span here</p>
        <p><abbr title="Hope This Helps">HTH</abbr></p>
        
        • FIND = (?s)<p><span (.*?class=".*?".*?)>(.*?)</span></p>
        • REPLACE = <p $1>$2</p>
        • MODE = regular expression
        <p class="my_home">My text here.</p>
        <p class="other_class">Other text
        there.</p>
        <p>No span here</p>
        <p><abbr title="Hope This Helps">HTH</abbr></p>
        

        So my solution will change any class of <p><span class="...">...</span></p>, but other similar constructs (without a span) will not change. I made sure it would work even for multi-line paragraphs (again, not in your spec, but that’s a reasonable assumption in HTML paragraphs).

        1 Reply Last reply Reply Quote 2
        • Robin CruiseR
          Robin Cruise
          last edited by

          @PeterJones said in Regex: Replace tag with another tag from text:

          (?s)<p><span (.?class=".?".?)>(.?)</span></p>

          yes, is good your solution. Thank you.

          But in my case, it must be replace only that particular tag <span class="my_home"> :) How can be done ?

          hasan123H 1 Reply Last reply Reply Quote 0
          • hasan123H
            hasan123 @Robin Cruise
            last edited by

            @Robin-Cruise said in Regex: Replace tag with another tag from text:

            ut in my case, it must be replace only that particular tag

            Hello, for that case you may try with:

            • FIND = (?s)<p><span (.*?class="my_home".*?)>(.*?)</span></p>
            • REPLACE = <p $1>$2</p>
            • MODE = regular expression
            1 Reply Last reply Reply Quote 3
            • Robin CruiseR
              Robin Cruise
              last edited by

              @hasan123 said in Regex: Replace tag with another tag from text:

              <p $1>$2</p>

              thank you very much

              hasan123H 1 Reply Last reply Reply Quote 0
              • hasan123H
                hasan123 @Robin Cruise
                last edited by

                thank you very much

                You are welcome

                1 Reply Last reply Reply Quote 1
                • First post
                  Last post
                The Community of users of the Notepad++ text editor.
                Powered by NodeBB | Contributors