Vicky Dev
Vicky Dev

Reputation: 2193

Regex find function by name whose declaration can be in any format

I have a javascript file like below (only posted few lines for example) opened in Gedit(3.10.2 and later)/Geany(1.26 and later) editor:

doFilter : function(){
        var filters = $$('#'+this.containerId+' .filter input', '#'+this.containerId+' .filter select');
        var elements = [];
        for(var i in filters){
            if(filters[i].value && filters[i].value.length) elements.push(filters[i]);
        }
        if (!this.doFilterCallback || (this.doFilterCallback && this.doFilterCallback())) {
            this.reload(this.addVarToUrl(this.filterVar, encode_base64(Form.serializeElements(elements))));
        }
    },
resetFilter = function(){
    this.reload(this.addVarToUrl(this.filterVar, ''));
},
checkCheckboxes : function(element){
    elements = Element.select($(this.containerId), 'input[name="'+element.name+'"]');
    for(var i=0; i<elements.length;i++){
        this.setCheckboxChecked(elements[i], element.checked);
    }
},
function openGridRow(grid, event){
    var element = Event.findElement(event, 'tr');
    if(['a', 'input', 'select', 'option'].indexOf(Event.element(event).tagName.toLowerCase())!=-1) {
        return;
    }

    if(element.title){
        setLocation(element.title);
    }
}

In this file as one can see, javascript functions are declared in various formats, now I want to find a function by it's name, and I don't know in which format it's declaration will be.

How can I do this by regex ?

I have tried function.*{|\w* \= function.*{|jquery.*{ but this gives me all functions, I want to find a particular function by name for example doFilter but I don't know in which format it would be declared.

Upvotes: 1

Views: 189

Answers (2)

Wesley Smith
Wesley Smith

Reputation: 19571

You could probably do better, and this is far from fully tested, but this seems to work:

/(function)?\\s*doFilter\\s*(:|=)?\\s*(function)?\\s*\\(/

regex 101 test

This would match the following formats:

doFilter : function(){
},

doFilter = function(){
},

function doFilter(grid, event){   
}

Upvotes: 0

revo
revo

Reputation: 48751

One thing that these kinds of Regular Expressions need is matching nested braces in sequence - known as recursion.

But I'm not going to write recursive match as I don't know if you are going to implement this in your JS app or you are working with your editor search functionality only.

(?:doFilter\s*[:=]\s*function|function\s*doFilter)\([^)]*\)\s*{((?:[^{}]+|{[^}]+})*)}

Live demo

Above regex doesn't need a complete explanation, it is easy to read but I will explain the part that probably may be ambiguous:

{               # Start of function body
  (               # Start of capturing group (1)
    (?:           # Start of non-capturing group (a)
      [^{}]+        # Match characters except { and }
      |             # OR
      {[^}]+}       # Match a block of braces {...}
    )*            # Zero or more times (greedy) - End of non-capturing group (a)
  )               # End of capturing group (1)
}               # End of function body

Note

This regex is supposed to be used on JavaScript codes (that specific function body) which does not have { and } in a comment line like // { comment (unpaird) or in quoted strings string == '{'.

If you need them you may want to look at this answer of me and grab parts that are needed.

Upvotes: 1

Related Questions