That gave me more elements than I wanted.  It would have been too hard
to then step through the array and work out that was actually meant to
be executed. Here's what I currently have working.  I know that it is
a hack way of doing it but it seems to be the only way of getting an
array instead of just the first / last match:

var scripts=(cont.indexOf("<script>")!=-1) ?
cont.substring(cont.indexOf("<script>")).split("</script>") : [];
for (var i=0; i<scripts.length; i++)
scripts[i]=scripts[i].substring(scripts[i].indexOf("<script>")+8);

// this seems to be needed as the last element is always wrong.
if (scripts.length>1) scripts.length--;

for (var i=0; i<scripts.length; i++) alert(scripts[i]);

If someone knows of a better way, please help me.  It seems a very dodgy
method of simply extracting tags from a string.

I should have a working example up in a few days.  It uses a java
applet to download a url and then reads through it and extracts the
<title>, <body> and <script> tags.  This should allow for dynamic
loading of content without the need for Loadpanel and simply extending
the setHTML method of a dynlayer.

Tarun Ramakrishna Elankath wrote:

> Hey, heres where a perl coder like me could help you.
> You would have to use a non-greedy quantifier.
> However I believe IE's pathetic RegExp object doesnt support non-greedy
> quantifiers like .*?
> Thus you could use an alternative approach like shown below :
> ..
> var contents = new String(" <script> bingo <\/script><script> tringo
> <\/script>   <script> Bye <\/script> GoodBye <body>Done!<\/body> "); //for
> example
>
> var scripts = contents.split(/(\s*<script>|<\/script>\s*)/);
>  alert(scripts[0]);
>  alert(scripts[1]);
>  alert(scripts[2]);
>  alert(scripts[3]);
> ..
>
> -----Original Message-----
> From: [EMAIL PROTECTED]
> [mailto:[EMAIL PROTECTED]]On Behalf Of Michael
> Pemberton
> Sent: Friday, February 09, 2001 10:20 AM
> To: [EMAIL PROTECTED]
> Subject: [Dynapi-Help] Regular Expressions
>
> Does anyone now how I can achieve the following:
> I want to parse the contents of a file and extract the <script> tags.
> I'm currently using the following code:
>     var scripts=/<script>\s*(.*)\s*<\/script>/i.exec(contents); // where
> contents = file contents
>
> If there is only one <script> tag everything works fine.  If there are
> two, the result is stuffed.
> e.g..
>     var contents = "<script> alert('test 1') </script> test html
> <script> alert('test 2') </script>";
>
> scripts returns "alert('test 1') </script> test html <script>
> alert('test 2')"
>
> It should have returned and array of elements containing each of the
> individual script tags code.
>
> Any help would be greatly appreciated, thanx.
>
> --
> Michael Pemberton
> [EMAIL PROTECTED]
> ICQ: 12107010
>

--
Michael Pemberton
[EMAIL PROTECTED]
ICQ: 12107010


_______________________________________________
Dynapi-Help mailing list
[EMAIL PROTECTED]
http://lists.sourceforge.net/lists/listinfo/dynapi-help

Reply via email to