I'm having one heck of a time trying to match a tag. It looks right to me (and checks out in all of the Regex Tester webapps I've tried) but does not work when I put it to use. The following is the code (with site address and name replaced with "Awesome Site") of the web page, and after that is the Javascript I'm using to try to match it. I am trying to match the Table Cell with the ID "myAlerts". Currently, it alerts null.

<table cellpadding="1" cellspacing="0" width="100%">
						<tbody><tr>
							<td id="myAlerts">
							<a href="/home.php?nc=1"><b>Awesome Site Inc</b> has news.<br></a><a href="/home.php?nc=1"><b>Awesome Site Inc</b> has a comic.<br></a><a href="http://www.awesomesite.com/archive/?nc=1"><b>Awesome Site Inc</b> has a new video.<br></a><a href="/members/messaging/">You have <b>1</b> new message.</a><br><a href="/members/messaging/">You have <b>1</b> new friend request.</a><br><a href="/members/comments/index.php">You have <b>10</b> new personal comments.</a><br><a href="/members/journal/entry.php?id=2059183">You have <b>1</b> new journal comment.</a><br><a href="/members/journal/entry.php?id=2059185">You have <b>1</b> new journal comment.</a><br><a href="/members/images/image.php?id=2239675">You have <b>1</b> new image comment.</a><br><a href="/members/clearAllAlerts.php"><b>Clear all alerts.</b></a>							</td>

						</tr>
						</tbody></table>
function test(){
	var response=document.body.innerHTML
	var regAlert=/<td id='myAlerts'>\n?(.*?)<\/td>/mi;
	alert(response.match(regAlert));
}

Of little note: I am executing this through the Greasemonkey extension for Firefox, so my method of setting the "response" variable is slightly different. In practice, it captures the entire HTML of the page, not just the contents of the Body tag. Also, the alert() is a simple replacement for where I append a DIV to the current page. For the purposes of troubleshooting, that info is irrelevant and I have already tested those components to be sure they're working properly. I'm just stating them here in anticipation that people will want to know what exactly I'm trying to accomplish.

Recommended Answers

All 3 Replies

If the script is embedded in the web page, it might be finding and matching itself.

not quite that simple. It's executing through the Greasemonkey extension or Firefox, and the regular expression is being matched against the HTML of an entirely separate page.

I've discovered that the failure arises with several "newline" characters in the HTML. this cannot be changed, so I guess I need to figure out how to match literally anything. A period (.) by itself matches anything except a newline, so I tried (.|\n)*? to attempt to match anything, but it didn't work. I'm well and truly stuck.

not quite that simple. It's executing through the Greasemonkey extension or Firefox, and the regular expression is being matched against the HTML of an entirely separate page.

I've discovered that the failure arises with several "newline" characters in the HTML. this cannot be changed, so I guess I need to figure out how to match literally anything. A period (.) by itself matches anything except a newline, so I tried (.|\n)*? to attempt to match anything, but it didn't work. I'm well and truly stuck.

Very accurate discussion here:

http://simonwillison.net/2004/Sep/20/newlines/

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.