For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > March 2006 > Javascript Regex to Perl Regex









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Javascript Regex to Perl Regex
Kevin Old

2006-03-23, 6:58 pm

Hello everyone,

I have a piece of javascript that formats a chunk of javascript into a
"pretty print" version of it. I'd like to have a command line version
of this, but can't seem to get the regexes to work in Perl. Just a
long shot, but can anyone help with the conversion of them from JS to
Perl?

Here's the code:

<html>
<body>

<script type=3D"text/javascript">
function prettyScript(code) {
try {
var fcode =3D (new Function(code)).toString();
fcode =3D fcode.replace(/^\s*function\s+(?:\w+\(\)\s*)?\s*\{\s*\n/, "=
");
fcode =3D fcode.replace(/\s*}\s*$/, "");
return fcode;
} catch (e) {
return "SYNTAX ERROR: " + e.message;
}
}
</script>
<form action=3D""
onsubmit=3D"this.elements.output.value =3D
prettyScript(this.elements.input.value);
return false;">
<textarea id=3D"input" cols=3D"80" rows=3D"5">"code to format here"</textar=
ea><br>
<input type=3D"submit" value=3D"format"><br>
<textarea name=3D"output" cols=3D"80" rows=3D"10" readonly=3D"readonly">Out=
put
here</textarea><br>
</form>

</body>
</html>

Any help is appreciated,
Kevin
--
Kevin Old
kevinold@gmail.com
Timothy Johnson

2006-03-23, 6:58 pm


At first glance it looks like this should work:

$fcode =3D~ s/^\s*function\s+(?:\w+\(\)\s*)?\s*\{\s*\n//gi;

but I don't have any code to test it on. What do you expect to happen,
and what is really happening?

-----Original Message-----
From: Kevin Old [mailto:kevinold@gmail.com]=20
Sent: Thursday, March 23, 2006 11:17 AM
To: Perl Beginners
Subject: Javascript Regex to Perl Regex

Hello everyone,

I have a piece of javascript that formats a chunk of javascript into a
"pretty print" version of it. I'd like to have a command line version
of this, but can't seem to get the regexes to work in Perl. Just a
long shot, but can anyone help with the conversion of them from JS to
Perl?

Here's the code:

<html>
<body>

<script type=3D"text/javascript">
function prettyScript(code) {
try {
var fcode =3D (new Function(code)).toString();
fcode =3D =
fcode.replace(/^\s*function\s+(?:\w+\(\)\s*)?\s*\{\s*\n/,
"");
fcode =3D fcode.replace(/\s*}\s*$/, "");
return fcode;
} catch (e) {
return "SYNTAX ERROR: " + e.message;
}
}
</script>
<form action=3D""
onsubmit=3D"this.elements.output.value =3D
prettyScript(this.elements.input.value);
return false;">
<textarea id=3D"input" cols=3D"80" rows=3D"5">"code to format
here"</textarea><br>
<input type=3D"submit" value=3D"format"><br>
<textarea name=3D"output" cols=3D"80" rows=3D"10" =
readonly=3D"readonly">Output
here</textarea><br>
</form>

</body>
</html>

Any help is appreciated,
Kevin
--
Kevin Old
kevinold@gmail.com

--=20
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>



Kevin Old

2006-03-23, 6:58 pm

Here's what I have.

# Open js file
open JSFILE, "$ARGV[0]" or die "can't open file: $ARGV[0]";
my $js =3D do { local( $/ ); <JSFILE> };
close JSFILE;

# Clean js
$js =3D~ s/^\s*function\s+(?:\w+\(\)\s*)?\s*\{\s*\n//gi;
$js =3D~ s/\s*}\s*$//gi;

# Write new js file
open JSOUTFILE, ">out_$ARGV[0]" or die "can't open file: out_$ARGV[0]";
print JSOUTFILE $js;
close JSOUTFILE;

If you cut and past the HTML and run it in a browser it will turn code
like this:

function wR(_1,_2,_3,_4){
var _5=3Ddocument.getElementById("w"+_1);
var _4=3D_4||window.event;
sbHandler.prediction=3D_3;
RatingManager.handleMouseover(_4,_5);
}

into this:

function wR(_1, _2, _3, _4) {
var _5 =3D document.getElementById("w" + _1);
var _4 =3D _4 || window.event;
sbHandler.prediction =3D _3;
RatingManager.handleMouseover(_4, _5);
}

I'm trying to at least duplicate that, and add a few other
improvements. When I run my script it's not changing anything.

Any suggestions?

Kevin

On 3/23/06, Timothy Johnson <tjohnson@zonelabs.com> wrote:
>
> At first glance it looks like this should work:
>
> $fcode =3D~ s/^\s*function\s+(?:\w+\(\)\s*)?\s*\{\s*\n//gi;
>
> but I don't have any code to test it on. What do you expect to happen,
> and what is really happening?
>
> -----Original Message-----
> From: Kevin Old [mailto:kevinold@gmail.com]
> Sent: Thursday, March 23, 2006 11:17 AM
> To: Perl Beginners
> Subject: Javascript Regex to Perl Regex
>
> Hello everyone,
>
> I have a piece of javascript that formats a chunk of javascript into a
> "pretty print" version of it. I'd like to have a command line version
> of this, but can't seem to get the regexes to work in Perl. Just a
> long shot, but can anyone help with the conversion of them from JS to
> Perl?
>
> Here's the code:
>
> <html>
> <body>
>
> <script type=3D"text/javascript">
> function prettyScript(code) {
> try {
> var fcode =3D (new Function(code)).toString();
> fcode =3D fcode.replace(/^\s*function\s+(?:\w+\(\)\s*)?\s*\{\s*\n/,
> "");
> fcode =3D fcode.replace(/\s*}\s*$/, "");
> return fcode;
> } catch (e) {
> return "SYNTAX ERROR: " + e.message;
> }
> }
> </script>
> <form action=3D""
> onsubmit=3D"this.elements.output.value =3D
> prettyScript(this.elements.input.value);
> return false;">
> <textarea id=3D"input" cols=3D"80" rows=3D"5">"code to format
> here"</textarea><br>
> <input type=3D"submit" value=3D"format"><br>
> <textarea name=3D"output" cols=3D"80" rows=3D"10" readonly=3D"readonly">O=

utput
> here</textarea><br>
> </form>
>
> </body>
> </html>
>
> Any help is appreciated,
> Kevin
> --
> Kevin Old
> kevinold@gmail.com
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>
>
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>
>



--
Kevin Old
kevinold@gmail.com
Timothy Johnson

2006-03-23, 6:58 pm

Using YAPE::Regex::Explain, this is how the first regex is being
interpreted:


The regular expression:

(?-imsx:s/^\s*function\s+(?:\w+\(\)\s*)?\s*\{\s*\n//gi)

matches as follows:

NODE EXPLANATION
----------------------------------------------------------------------
(?-imsx: group, but do not capture (case-sensitive)
(with ^ and $ matching normally) (with . not
matching \n) (matching whitespace and #
normally):
----------------------------------------------------------------------
s/ 's/'
----------------------------------------------------------------------
^ the beginning of the string
----------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0 or
more times (matching the most amount
possible))
----------------------------------------------------------------------
function 'function'
----------------------------------------------------------------------
\s+ whitespace (\n, \r, \t, \f, and " ") (1 or
more times (matching the most amount
possible))
----------------------------------------------------------------------
(?: group, but do not capture (optional
(matching the most amount possible)):
----------------------------------------------------------------------
\w+ word characters (a-z, A-Z, 0-9, _) (1 or
more times (matching the most amount
possible))
----------------------------------------------------------------------
\( '('
----------------------------------------------------------------------
\) ')'
----------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0
or more times (matching the most amount
possible))
----------------------------------------------------------------------
)? end of grouping
----------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0 or
more times (matching the most amount
possible))
----------------------------------------------------------------------
\{ '{'
----------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0 or
more times (matching the most amount
possible))
----------------------------------------------------------------------
\n '\n' (newline)
----------------------------------------------------------------------
//gi '//gi'
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------



One thing I'm noticing is that this should only match if you don't pass
anything to the function. I've gotta run, but maybe this is a place to
start.


-----Original Message-----
From: Kevin Old [mailto:kevinold@gmail.com]=20
Sent: Thursday, March 23, 2006 11:35 AM
To: Timothy Johnson
Cc: Perl Beginners
Subject: Re: Javascript Regex to Perl Regex

Here's what I have.

<snip>

# Clean js
$js =3D~ s/^\s*function\s+(?:\w+\(\)\s*)?\s*\{\s*\n//gi;
$js =3D~ s/\s*}\s*$//gi;

<snip>

If you cut and past the HTML and run it in a browser it will turn code
like this:

function wR(_1,_2,_3,_4){
var _5=3Ddocument.getElementById("w"+_1);
var _4=3D_4||window.event;
sbHandler.prediction=3D_3;
RatingManager.handleMouseover(_4,_5);
}

<snip>

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com