Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression to match JavaScript function

Is there a regular expression that matches JavaScript functions?

Like

function abc(var1, var2){
  ..
}

// and

abc : function(var1, var2){
  ...
},
like image 695
complez Avatar asked Nov 17 '10 12:11

complez


1 Answers

I know this question is 5 years old, but contrary to what everyone else has said, i have actually concocted a quite effective pattern for doing as you have asked. Albeit, quite complex, I have used this several times in my own projects and I've yet to have a hiccup... wish I had seen this question much sooner. Hope this helps (If not for you, hopefully for those who are searching for a similar solution)

function\s*([A-z0-9]+)?\s*\((?:[^)(]+|\((?:[^)(]+|\([^)(]*\))*\))*\)\s*\{(?:[^}{]+|\{(?:[^}{]+|\{[^}{]*\})*\})*\}

like image 101
Ja Superior Avatar answered Sep 30 '22 23:09

Ja Superior