Community
    • Login

    Regex: Replace tag with another tag from text

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    6 Posts 3 Posters 1.6k Views 2 Watching
    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 Offline
      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 Offline
        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 Offline
          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 Offline
            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 Offline
              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 Offline
                hasan123 @Robin Cruise
                last edited by

                thank you very much

                You are welcome

                1 Reply Last reply Reply Quote 1

                Hello! It looks like you're interested in this conversation, but you don't have an account yet.

                Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

                With your input, this post could be even better 💗

                Register Login
                • First post
                  Last post
                The Community of users of the Notepad++ text editor.
                Powered by NodeBB | Contributors