#!/usr/bin/perl use strict; use re 'eval'; my $Jscript = join (" ",); # Extract function foo from Jscript my $content = _extract("foo",$Jscript); sub _extract { my ($funct,$Jscript) = @_; #regexes to match all but starting positions of strings and comments my $double_quote_str = qr!(\\.|[^"\\])*"!; my $single_quote_str = qr!(\\.|[^'\\])*'!; my $comment1 = qr![^\n\r]*!; my $comment2 = qr!.*?\*/!s; #now match complete strings or comments..whatever matches first will 'eat' others my $escape_strings = qr! ( '(?{$start='sing_quot'})| "(?{$start='dbl_quot'})| //(?{$start='comment1'})| /\*(?{$start='comment2'}) ) (?(?{$start eq 'sing_quot'})$single_quote_str) (?(?{$start eq 'dbl_quot'})$double_quote_str) (?(?{$start eq 'comment1'})$comment1) (?(?{$start eq 'comment2'})$comment2) !x; my ($round,$curly); # match balanced nested round braces ..escape strings and comments $round = qr! \(( $escape_strings | [^()]|(??{$round}) )* \) !x; # match balanced nested curly braces ..escape strings and comments $curly = qr! { ( $escape_strings | [^{}]|(??{$curly}) )* } !x; if ($Jscript =~ m!(\bfunction\s+$funct\s*$round\s*$curly)!) { print "Matched :\n",$1; } } __DATA__ //.. function foo goes here .......