Re-use, concat those html calc fields with regex

I’ve got a number of calculations that return HTML for use in forms. There are also some times when I want to build a larger html content block for a report or such that utilizes these calculations. Rather than repeat myself and re-code the calc, I wrote a little helper method that uses regex to lop off any wrapping html/body tags from the calc result, so I can nest it inside a larger html document.

Just thought someone else might find it useful:

var s = arguments[0];
s = s.replace(/<html[^>]*>(.|\s)*<body[^>]*>/i, '');
s = s.replace(/<\/body>(.|\s)*<\/html>.*/i, '');
return s;

Let me know if anyone notices any flaws in my regex, but I think it should handle just about any combination of tags/attributes.

greg.

Great contribution. I love regex, regex rules :)

I would add the below line to remove the doctype declaration so one can read in an external file as well.

s = s.replace(/]*>/i, ‘’);

Good point. But, actually, there’s really nothing I could possibly want before the opening body tag come to think of it. So I could simplify the first pattern to:

(.|\s)<body[^>]>

greg.

In case of plain html you are right but what when you have a php import declared?