1

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.

Vicky Dev
  • 1,893
  • 2
  • 27
  • 62

2 Answers2

1

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.

Community
  • 1
  • 1
revo
  • 47,783
  • 14
  • 74
  • 117
0

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){   
}
Community
  • 1
  • 1
Wesley Smith
  • 19,401
  • 22
  • 85
  • 133
  • This answer matches calls to `doFilter` as well. – revo Aug 20 '16 at 12:31
  • @revo, ah indeed. Yeah, Im thinking its not going to be possible using the function name just once. Oh well. – Wesley Smith Aug 20 '16 at 20:24
  • @revo Yeah, that could still potentially match a call to doFilter https://regex101.com/r/vP5hR7/1 though it wouldnt be too likely – Wesley Smith Aug 20 '16 at 21:39
  • I used `.` to demonstrate it shouldn't include newlines (should not go further beyond one line). If newlines are somewhere between function declaration or multiple lines are compressed into one line then this regex is not made for it. That's why I call it nasty. – revo Aug 20 '16 at 22:10
  • Agreed, I figured that's why you didn't use it – Wesley Smith Aug 20 '16 at 22:11