Advertisement

12.07.2007 at 02:56AM PST, ID: 23008284
[x]
Attachment Details

JavaScript RegExp - Match HTML Tag Containing Line Breaks

Asked by wwarby in Regular Expressions, JavaScript

Tags: javascript, tag, match, line, break

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.Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
<html>
	<head>
		<title>Regular Expression Test</title>
	</head>
	<body>
		<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>');
			}
		</script>
	</body>
</html>
 
Loading Advertisement...
 
[+][-]12.07.2007 at 03:18AM PST, ID: 20426624

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]12.07.2007 at 04:51AM PST, ID: 20426998

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]12.07.2007 at 05:40AM PST, ID: 20427246

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]12.07.2007 at 06:10AM PST, ID: 20427421

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Regular Expressions, JavaScript
Tags: javascript, tag, match, line, break
Sign Up Now!
Solution Provided By: ddrudik
Participating Experts: 2
Solution Grade: A
 
 
[+][-]12.07.2007 at 06:39AM PST, ID: 20427620

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]12.07.2007 at 06:48AM PST, ID: 20427691

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628