I want to replace \n with <br /> in a string. But not that simple. There shouldn’t be a </h2> or [/h2] before \n if it is to be raplaced.
For example put a <br /> between “Something” and “Another thing” in this example:
Something
Another thing
But do nothing in this example:
<h2>Something</h2>
<p>Another thing</p>
I come up with (?<!\]|\>)\n working on this .NET Regex Tester. Something to be replaced is found in the first example but not in the second as expected. However, I couldn’t make this nice regex work like this in my C# code. The following code replaces all \ns regardless whether it follows a </h2> or not.
Cevrilen = Regex.Replace(Cevrilen, @"(?<!\]|\>)\n", "<br />");
How can I replace breaks just if they are not followed by a </h2> tag?
That should do it (using a negative look-behind to disqualify
</h2>or[/h2]preceding a\n)