I’m trying to write a regex that will capture a function in JavaScript - although the language in which the function is written really doesn’t matter.

I have this JavaScript code:

function firstFunc(){ if(true){ alert('testF'); } elseif(false) alert('testf'); } function submitme(){ if(true){ alert('test1'); } elseif(false){ alert('test2'); } elseif(false){ alert('test3'); } } function submitmeAlso(){ if(true){ alert('test'); } elseif(false) alert('test'); }

I’m trying to capture the entire “submitme()” function from the word “function” all the way to the closing curly bracket of just that function.

I’ve come across a recursive pattern that will capture a pair of outer brackets and all nested ones, but once you add the beginning text - the word ‘function’, the function name, and the opening and closing argument brackets - problems ensue.

This may not be that hard, but I've had a heck of a time writing the right pattern.

Any help (an example pattern) would be greatly appreciated.


In reply to Recursive Regular Expression Help by me

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.