Javascript Exec() Method For Regular Expression Matching

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
	<title>Javascript Regular Expression Exec()</title>
 
	<script type="text/javascript">
 
		// Define the text that we are going to search.
		// This text was taken from Sir Mix-a-Lot's hit
		// single, "I Like Big Butts".
		var strText = "\
			I like big butts and I can not lie \
			You other brothers can't deny \
			That when a girl walks in with an itty bitty waist \
			And a round thing in your face \
			You get sprung \
			Wanna pull up tough \
			Cuz you notice that butt was stuffed \
			Deep in the jeans she's wearing \
			I'm hooked and I can't stop staring \
			Oh, baby I wanna get with ya \
			And take your picture \
			";
 
 
		// Get a pattern that we want to search on. This
		// defines certain modifiers and the words that
		// the modify.
		var rePattern = new RegExp(
			"(big|round|bitty)(?:\\s+)([^\\s]+)",
			"gi"
			);
 
	</script>
</head>
<body>
 
	<script type="text/javascript">
 
		// Define our match array. This will be populated for
		// each iteration of the Exec() method.
		var arrMatch = null;
 
 
		// Keep looping over the target text while we can
		// find matches. If no matches can be found,
		// arrMatch is null and will end the while loop.
		while (arrMatch = rePattern.exec( strText )){
 
			document.write(
				arrMatch[ 1 ].toUpperCase() +
				" modifies " +
				arrMatch[ 2 ].toUpperCase() +
				"<br />"
				);
 
 
			// Explore the modified properties of both
			// the returned array as well as the regular
			// expression object. The returned array,
			// unlike traditional Javascript arrays, is
			// given pattern-matching related properties.
 
 
			document.write(
				"........ " +
				"Phrase: " +
				arrMatch[ 0 ] +
				"<br />"
				);
 
			document.write(
				"........ " +
				"Start Index: " +
				arrMatch.index +
				"<br />"
				);
 
			document.write (
				"........ " +
				"End Index: " +
				rePattern.lastIndex +
				"<br />"
				);
 
			document.write (
				"........ " +
				"Index Substring: " +
				arrMatch.input.substring(
					arrMatch.index,
					rePattern.lastIndex
					) +
				"<br /><br />"
				);
		}
 
	</script>
 
</body>
</html>

For Cut-and-Paste