I have user-submitted text via form that could have multiple blocks of text in pre tags. I need to remove all newlines, without removing the newlines in the PRE tags, and preserve any other user formatting.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You will need to use conditional subpatterns here. Assuming
<pre>and</pre>tags are balanced, following code should work for you:OUTPUT
As you can see
\noutside<pre>and</pre>have been removed.This code searches for 0 or more occurrences of text between
<pre>tag in the string and if found then grabs text until</pre>tag. Single iteration of search stops when first\nis found and and then it replaces the matched text with first captured group (i.e. text that came before\n).