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