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\Rwith nothing.Essentially, everything after the
\Kis what you want to remove, and everything before the\Kis 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
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