Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Extracting javascript from a string

Status
Not open for further replies.

Deleted

Technical User
Jul 17, 2003
470
US
I am looking to extract javascript's from a string like the example below. I know there are several modules out there that will do this, but this is not an option for me.

my $variable = qq|
<html>
<head>
<title>test</title>

<script language="javascript">
alert('welcome');
</script>

<script language="javascript">
document.write('hello');
</script>

</head>
<body color="000000">

blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah

</body>
</html>
|;

Needs to be left with - to be parsed later on

<script language="javascript">
alert('welcome');
</script>

<script language="javascript">
document.write('hello');
</script>

Thank you in advance...

M. Brooks
X Concepts LLC
 
mbrooks,

you've been around here long enough to know better

were de code maaannn?

--Paul

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ...
 
Code:
[b]#!/usr/bin/perl[/b]

undef $/;

$_ = <DATA>;

@scripts = $_ =~ m|(<script language="javascript">[^/]*</script>)|sg;

print join ("\n", @scripts);
 
[blue]__DATA__
<html>
<head>
<title>test</title>

<script language="javascript">
alert('welcome');
</script>

something we don't want inbetween!

<script language="javascript">
document.write('hello');
</script>

</head>
<body color="000000">

blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, blah

</body>
</html>[/blue]

outputs:-

[red][tt]<script language="javascript">
alert('welcome');
</script>
<script language="javascript">
document.write('hello');
</script>[/tt][/red]


Kind Regards
Duncan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top