SHARE
TWEET

Untitled

a guest Oct 21st, 2019 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Return array, which contains match arrays
  3. Groups:
  4.   [0] - whole function
  5.   [1] - function name
  6.   [2] - function parameters string
  7.   [3] - function body (excluding brackets)
  8. Or use named groups (?<groupName>...) if ES2018
  9. */
  10. const definedJsFunctionsRegex = /function\s*([A-z0-9]+)?\s*\(([^)(]+|\((?:[^)(]+|\([^)(]*\))*\))*\)\s*\{((?:[^}{]+|\{[^}{]*\}|[^}]|\}(\"|\'|\`))*)*\}/g;
  11. const testStr = `
  12. function a (a,b) { hfgdhfhjg
  13.   fdgdhfytr
  14.   confsf
  15.   dfdasg   
  16.   }
  17.  
  18.  
  19.   function test() {
  20.   fdsf
  21.   consol.gkgd("fsdfs");
  22.   const a = function(m) {fuck you};
  23.   a();
  24.   return 1
  25.   }
  26.  
  27.   function(fds) { obj = {} return "}" }
  28.  
  29.  
  30.   function r () { obj = {}; a = []; }
  31.  
  32.   function test(float, best) {
  33.   console.log("Well");
  34.   }
  35.  
  36.   function a(){console.log('a')} function b(){console.log('b')}
  37. `;
  38.  
  39. let matches = [...testStr.matchAll(definedJsFunctionsRegex)];
  40. console.log(matches);
  41. /*
  42. Example output:
  43.  
  44. (7) […]
  45. 0: Array(5) [ "function a (a,b) { hfgdhfhjg\n  fdgdhfytr\n  confsf \n  dfdasg  \t\n  }", "a", "a,b", … ]
  46. 1: Array(5) [ "function test() {\n  fdsf\n  consol.gkgd(\"fsdfs\");\n  const a = function(m) {fuck you};\n  a();\n  return 1\n  }", "test", undefined, … ]
  47. 2: Array(5) [ "function(fds) { obj = {} return \"}\" }", undefined, "fds", … ]
  48. 3: Array(5) [ "function r () { obj = {}; a = []; }", "r", undefined, … ]
  49. 4: Array(5) [ "function test(float, best) { \n  console.log(\"Well\");\n  }", "test", "float, best", … ]
  50. 5: Array(5) [ "function a(){console.log('a')}", "a", undefined, … ]
  51. 6: Array(5) [ "function b(){console.log('b')}", "b", undefined, … ]
  52. length: 7
  53. <prototype>: Array []
  54. debugger eval code:31:9
  55.  
  56.  
  57. */
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top