Template

L

Lee

Hi,
I have this template file that I am opening in my script.

<tr>
<td>{var1}</td>
<td>{var2}</td>

<td>
<table>
<tr>
<td>Name: {name3}</td>
<td>Age: {age3}</td>
</tr>
</table>
</td>

</tr>

Are there any good regular expressions to grab each tag and its
contents? I'm doing this in javascript, but I'm guessing that there
are some really good regex experts in this group.
If there are any perl, php, or javascript libraries out there for this
sort of thing, I'd like to know about it!
 
H

Henry Law

Lee said:
Hi,
I have this template file that I am opening in my script.

<tr> ....
</tr>

Are there any good regular expressions to grab each tag and its
contents?

General advice here is not to try to parse HTML or XML with regexes;
it's a great deal harder than it looks and there are much better ways to
do it.
If there are any perl, php, or javascript libraries out there for this
sort of thing, I'd like to know about it!

Here's one: http://search.cpan.org/search?query=parse+html&mode=all

And another you might find useful, in the context of your post:
http://search.cpan.org/search?query=html+template&mode=all
 
T

Tad McClellan

Lee said:
<tr>
<td>{var1}</td>
<td>{var2}</td>

<td>
<table>
<tr>
<td>Name: {name3}</td>
<td>Age: {age3}</td>
</tr>
</table>
</td>

</tr>

Are there any good regular expressions to grab each tag and its
contents?


No. But here is a bad one that grabs the contents of all tags
in _your_ data (it can be easily broken by other legal HTML data though):

foreach my $tag ( /(<[^>]+>)/g ) {
print "$tag\n";
}

But I expect you don't really want what you've asked for, you
probably want the contents of *element* rather than tags. For
that you should...

If there are any perl, php, or javascript libraries out there for this
sort of thing, I'd like to know about it!


.... use a module that understands HTML data when you need to
process HTML data:

http://search.cpan.org/search?query=HTML+parser&mode=all
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top