How to combine a multi-line html element into one line?
-
I don’t know why I’m having trouble with this, but suppose I have the following:
<p>Lorem ipsum dolor sit amet, libero turpis non cras ligula, id commodo, aenean est in volutpat amet sodales, porttitor bibendum facilisi </p> <p>Donec vivamus. Vel donec et scelerisque vestibulum. Condimentum aliquam, mollit magna velit nec, tempor cursus vitae sit aliquet neque purus.</p> <p>Iaculis et dui ullamcorper, non egestas condimentum dui phasellus. Sit non mattis a, leo in imperdiet erat nec pulvinar. Ornare massa justo</p> <p>Rhoncus lacinia. Imperdiet nulla sem fringilla, purus enim amet, nascetur faucibus, adipiscing neque ut bibendum, at felis nec in. Mauris</p>
…and I want to put the contents of each
<p>
element onto the same line:<p>Lorem ipsum dolor sit amet, libero turpis non cras ligula, id commodo, aenean est in volutpat amet sodales, porttitor bibendum facilisi </p> <p>Donec vivamus. Vel donec et scelerisque vestibulum. Condimentum aliquam, mollit magna velit nec, tempor cursus vitae sit aliquet neque purus.</p> <p>Iaculis et dui ullamcorper, non egestas condimentum dui phasellus. Sit non mattis a, leo in imperdiet erat nec pulvinar. Ornare massa justo</p> <p>Rhoncus lacinia. Imperdiet nulla sem fringilla, purus enim amet, nascetur faucibus, adipiscing neque ut bibendum, at felis nec in. Mauris</p>
I have tried something like search: “
(<p.*?)(?<!<\/p>)\R +
” and replacing it with "\1
", but besides the fact that it won’t do multiple lines at once (I have to keep repeating the search/replace), the main issue is that if there is a line that starts with a<p>
and ends with a</p>
, it matches the whole line and whatever comes after it.What’s the best way to construct this search and replace?
-
This is a classic case of guy038’s patented “replace in a specific zone of text” regex (follow link for an explanation of how this regex works).
Simply replace
(?s-i)(?:<p>|(?!\A)\G)(?:(?!</p>).)*?\K\R
with nothing.Essentially, everything after the
\K
is what you want to remove, and everything before the\K
is ensuring that the replacements only happen within<p>
tags. -
@pbarney said in How to combine a multi-line html element into one line?:
What’s the best way to construct this search and replace?
A similar question was posted some time ago here. I suggest reading that. As you seem to be proficient at regex hopefully you can adjust to suit your need.
Terry