Search code examples
javascriptjqueryregexeditorprototypejs

Regex to match any Javascript/jQuery function declaration line


I have following regex pattern for matching any function declaration of type function funcname() {.

function\s+(.*?)\)\s*{

But sometimes in Javascript/jQuery functions are declared in below ways too:

funcName = function() {

Or

jQuery.fn.funcName = function () {

I don't know how to ORify regex to allow all the above syntaxes to be matched. Braces maybe in nextline acording to code format rules.


Solution

  • Something like this should work:

    function.*{|\w* \= function.*{|jquery.*{
    

    The vertical bar character is how you "orify" in regex.

    And to deal with multiple lines you need to use the newline selector: \n

    For example, to match:

    function funcname() {
    
    }
    

    You might use:

    function.*\{\n*}