Okay, so I have tried using Parse::RecDescent and built a small parser for var statements... I'm having only one problem...

use Parse::RecDescent; $grammar = q { varStatement: 'var' statements endofvar statements: <leftop: statement comma statement> comma_values: <leftop: assignvalue comma assignvalue> statement: var_name (operator assignvalue)(?) assignvalue: equality | escapedRegex | escapedQuote | array_declaration | numeric_value | array_value | object_value | var_name array_declaration: 'new Array(' comma_values ')' array_value: array_name '[' integer ']' equality: '(' assignvalue equality_operator assignvalue + ')' var_name: /\w+/ { $return = "$item[1 +]" } array_name: /\w+/ object_value: /[A-Za-z0-9_.]+/ numeric_value: real_number | integer integer: /\d+/ real_number: /\d+\.?\d*/ escapedRegex: '__REGEX__' escapedQuote: '__QUOTE__' operator: '=' equality_operator: '===' | '==' | '!=' endofvar: ';' comma: ',' }; print "\n\n"; $parser = new Parse::RecDescent ($grammar) or die "*** Bad grammar!\n" +; foreach my $localDeclaredVar (@localDeclaredVars) { print "$localDeclaredVar\n"; my $test = $parser->varStatement($localDeclaredVar) or print "*** +Bad text!!!\n"; print "==>$test\n"; }

How do we grab the matched var_name? My goal is to match each variable name that was matched... but I've read the FAQ as much as I could handle and cannot determine that small fact....

The Input

var myTest1 = 1; var myTest2 = 2, myTest3 = 3, myTest4; var myTest5 = new Array(__QUOTE__,__QUOTE__), myTest6; var myTest7 =__REGEX__; var myTest8 = myTest5.x; var myTest9 = myTest[0], myTest10 = myTest[0]; var myTest11 = (myTest1 == myTest2); var myTest12 = (myTest1 == myTest2), myTest13 = 2; var myTest14 = (myTest1 == myTest2), myTest15; var myTest16 = new Array(1, 2); var myTest17, myTest18;

My Output

var myTest1 = 1; ==>; var myTest2 = 2, myTest3 = 3, myTest4; ==>; var myTest5 = new Array(__QUOTE__,__QUOTE__), myTest6; ==>; var myTest7 =__REGEX__; ==>; var myTest8 = myTest5.x; ==>; var myTest9 = myTest[0], myTest10 = myTest[0]; ==>; var myTest11 = (myTest1 == myTest2); ==>; var myTest12 = (myTest1 == myTest2), myTest13 = 2; ==>; var myTest14 = (myTest1 == myTest2), myTest15; ==>; var myTest16 = new Array(1, 2); ==>; var myTest17, myTest18; ==>;

As you can see, all that I get is the darn semicolon - the string that was left after all matching was successful... this is of course not what I want... Does anyone know how to solve this?


In reply to Re: Re: Regex for stripping variable names from a JavaScript file by Incognito
in thread Regex for stripping variable names from a JavaScript file by Incognito

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.