So someone actually posted a fantastic solution here How can I escape all code within <code></code> tags to allow people to post code?
The problem is that this only works if it's
In spite of my comment above, I'll endeavour to provide a regex for you to use. I would, however, emphatically not recommend doing this with regexes, but using an HTML parser instead.
Your regex should look a bit like this:
To break it down a bit,
\s* matches zero or more whitespace characters.
code matches the literal string "
.*? is a lazy match of zero or more characters. It will match everything (if anything) up to the end of the tag.
(.+?) is a capture group, containing a lazy match of one or more characters. If nothing else, your
<code> tags will never be completely blank (as there must be at least one character between them).
<\s*\/code\s*> matches the ending tag, with the possibility of included whitespace. Note that the slash (
/) character is escaped, as it has to be in just about every regex flavour under the sun.