Regex: Replace tag with another tag from text
-
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?
-
@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). - FIND =
-
@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 ? -
@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
- FIND =
-
-
thank you very much
You are welcome
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