Holy Cow!
The stuff you sent was a puzzle that needed figuring!
I hadnt understood what you required this for. Now that I do, I can suggest
a more elegant way that I thought of :
...
var contents = new String(" <title> asdf</title> <script> bingo
<\/script><script> tringo <\/script> <script> Bye <\/script> GoodBye
<body>Done!<\/body><script> yeah! <\/script> ");
var src= contents;
var tagOpen = "<script>";
var tagClose = "<\/script>";
var scripts = new Array();
var cnt = 0;
while(!/^\s*$/.test(src)) {
posOpen = src.indexOf(tagOpen);
posClose = src.indexOf(tagClose);
scripts[cnt++] = src.substring(posOpen+tagOpen.length,posClose);
src = src.substring(posClose+tagClose.length,src.length);
}
for (var i=0;i<scripts.length;i++) alert(scripts[i]);
..
You could encapsulate this code into a function that takes tagOpen,tagClose
and the array to put stuff into as parameters and call it from the main
code.
The most bugging thing about whole matter is that this would have been a
single line of code if we had non-greedy RegExp matching in IE5.0.
(Apparently 5.5 supports this.)
Anyhow it was an engaging mental exercise.
-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED]]On Behalf Of Michael
Pemberton
Sent: Friday, February 09, 2001 6:32 PM
To: [EMAIL PROTECTED]
Subject: Re: [Dynapi-Help] Regular Expressions
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
_______________________________________________
Dynapi-Help mailing list
[EMAIL PROTECTED]
http://lists.sourceforge.net/lists/listinfo/dynapi-help