bandybabboon
bandybabboon

Reputation: 2346

Regex to detect "function foo(){//with a comment"?

I have a JavaScript functionList parser which works, however it can't see the function if there are spaces before the brackets or if there is a comment in the function line:

 function parserworks(){ 
 function parserfails (){
 function parserfails(){//comments make the parsing fail 
 function parserfails ( v1:int; v2:byte ):int{

How can I detect and parse any word starting with "function" and ending with "(", so anything of the kind "function foo ( v1:int; v2:byte ):int // ,regardless of what's written after the (?

This is the parsing definition in .xml:

        <parser id="js_function" displayName="Javascript" commentExpr="((/\*.*?\*)/|(//.*?$))">
            <function
                mainExpr="((^|[\s]+|[;\}\.])([_A-Za-z][\w_]*\.)*[_A-Za-z][\w_]*[\s]*[=:]|^|[\s;\}]+)[\s]*function([\s]+[_A-Za-z]?[\w_]*\([^\)\(]*\)|\([^\)\(]*\))[\n\s]*\{"
                displayMode="$className->$functionName">
                <functionName>
                    <nameExpr expr="[_A-Za-z][\w_]*[\s]*[=:]|[_A-Za-z]?[\w_]*[\s]*\("/>
                    <nameExpr expr="[_A-Za-z]?[\w_]*"/>
                </functionName>
                <className>
                    <nameExpr expr="([_A-Za-z][\w_]*\.)*[_A-Za-z][\w_]*\."/>
                    <nameExpr expr="([_A-Za-z][\w_]*\.)*[_A-Za-z][\w_]*"/>
                </className>
            </function>
        </parser>

Upvotes: 0

Views: 38

Answers (1)

JBone
JBone

Reputation: 1826

    /function.*(\s+\()|(\/\/)/

try that one

Upvotes: 1

Related Questions