JavaScript RegExp - Match HTML Tag Containing Line Breaks

I need a regular expression that matches HTML tags which can contain line breaks / carriage returns. This is easy in Perl or PHP because the dot (any) token matches line breaks, but unfortunately it does not in JavaScript. The attached code demonstrates the problem Change the regular expression to make it match the <p> tag and it's contents to win the prize.
		<title>Regular Expression Test</title>
		<script language="javascript" type="text/javascript">
			var s;
			s += "<html>\n";
			s += "	<body>\n";
			s += "		<p>\n";
			s += "			Match this paragraph\n";
			s += "			even though it contains\n";
			s += "			line breaks\n";
			s += "		</p>\n";
			s += "	</body>\n";
			s += "</html>";
			var re = new RegExp('<p[^>]*>(.*)</p>', 'gim');
			var matches = s.match(re);
			if (matches && matches.length) {
				for (var x = 0; x < matches.length; x++) {
					document.write('<h3>Match found:</h3><xmp>' + matches[x] + '</xmp>');
			} else {
				document.write('<h3>No matches found!</h3>');

Open in new window

Who is Participating?
ddrudikConnect With a Mentor Commented:
var re = /<p[^>]*>([\S\s]*?)<\/p>/ig;

Note that you have a group () that you could also reference along with the match, if that's not required then you can remove () above.
Ramesh SrinivasTechnical ConsultantCommented:

Not sure what exactly you need here... but putting the \n (newline) regex will match the beginning of the paragraph.



Instead of ".*" in your pattern I would recommend you use "[\S\s]*?"
Note that the use of "?" is required to stop at the first enclosing "</p>" in case you have multiple "<p></p>" blocks in your HTML source.
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

wwarbyAuthor Commented:
Guys, thanks for your efforts so far but I'm afraid neither of these suggestions has worked in this particular example. Using the source code I provided, I substituted the following regular expressions:

<p[^>]*>(.*)\n</p> (from saleek)
<p[^>]*>([\S\s]*?)</p> (from ddrudik)

...but neither of them match the paragraph tag, at least not in the SpiderMonkey JS engine in Firefox 2.

The example I've given is just a simplification the real problem I'm working on which is a script that strips XML tags from text objects in a proprietary format in our web content management system. I have RegexBuddy and the expression I started with works fine until you emulate JavaScript because the dot can't cross line breaks. I've tried a lot of variations and so far drawn a blank.

I'm pretty desperate to get this script working so if I'm increasing the points to 500. Can I suggest saving the code snippet as an HTML file and testing in a browser - if you get the right regular expression in there it will write the matched code back to the screen.


wwarbyAuthor Commented:

Thank you very much - that's exactly what I was after. The group () in there since because where I will be using this expression is in a script that strips tags - I need to capture the content of the tags and remove the tags themselves. Where I was going wrong is using the RegExp object constructor rather than the inline constructor you have used. I tried the exact same regular expression constructed both ways - your one works, mine doesn't. I'll be accepting your solution with full marks either way, but I'd be very grateful for your thoughts on why one of these statements works when the other doesn't:

var re = /<p[^>]*>([\S\s]*?)<\/p>/ig;
var re = new RegExp('<p[^>]*>([\S\s]*?)<\/p>', 'ig');


It seems that \'s need to be escaped with \ as:
var re = new RegExp('<p[^>]*>([\\S\\s]*?)<\\/p>', 'ig');
wwarbyAuthor Commented:
Of course ;) I've hit that problem before - should have noticed it.

Thanks very much for this :)
Thanks for the question and the points.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.