Regular expression to match JavaScript function

Is there a regex that matches JavaScript functions?

how

function abc(var1, var2){
  ..
}

// and

abc : function(var1, var2){
  ...
},
+6
source share
5 answers

In JS, a function can contain functions (which, in turn, can contain functions, etc.):

x = function() {
  this.y = function() { /* ... */ };
  function z() { /* ... */ }
};

In addition, you can have string literals or comments that can contain (sub) strings that either look like functions:

var s = "function notAFunction(){}";
/* 
function alsoNotAFunction(){}
*/

or contain parts of functions that your regular expression would move:

function f() {
  var s = "not a closing bracket: } ";
}

So, to answer the question that the regular expression will correspond to functions in JS: it does not exist. You should / could use the right analyzer for this.

+3
source

, 5 , , , , , . , , , ... , . , ( , , , )

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

+8

JS ? , . Regex - .

?

RegEx , XHTML

HTML, HTML-?

+4

, function definations function usages .

esprima.

(: nodejs, javascript . npm i esprima clipboardy, index.js node index.js)

var esprima = require('esprima');
const clipboardy = require('clipboardy');
var program = '
const answer = 42;
function foo(){
    alert("hi")
}
';

//esprima.tokenize(program)
var entries = []
parsed = esprima.parseScript(program, {}, function (node, meta) {
            entries.push({
                start: meta.start.offset,
                end: meta.end.offset,
                node: node
            });
    })

clipboardy.writeSync(JSON.stringify(entries, null, 2));
console.log('full parsed data copied to clipboard!')

//find function calls
var functionCalls = entries.filter(e => e.node.type == "CallExpression")
console.log('function calls: ' + JSON.stringify(functionCalls.map (a => {return {name: a.node.callee.name, start: a.start, end: a.end}})))

//find alert function calls
var alertFunctionCalls = entries.filter(e => e.node.type == 'CallExpression' && (e.node.callee.name == 'alert'))
console.log('alert function calls: ' + JSON.stringify(alertFunctionCalls.map (a => {return {start: a.start, end: a.end}})))

//find function definations
var functionDeclarations = entries.filter(e => e.node.type == 'FunctionDeclaration')
console.log('function definations: ' + JSON.stringify(functionDeclarations.map (a => {return {name: a.node.id.name, start: a.start, end: a.end}})))

//find foo() function defination
var fooFunctionDeclaration = entries.filter(e => e.node.type == 'FunctionDeclaration' && e.node.id.name == 'foo')
console.log('foo function definations: ' + JSON.stringify(functionDeclarations.map (a => {return {start: a.start, end: a.end}})))

//remove alert function calls
var program2 = program
alertFunctionCalls.sort((a, b) => { return b.end - a.end }).forEach(n => {
    program2 = program2.substring(0, n.start) + program2.substring(n.end);
});
console.log('program after removing alert function calls: ' + program2)

0

, , , , , , , , . :

^(?:[\s]+)?(?:const|let|var|)?(?:[a-z0-9.]+(?:\.prototype)?)?(?:\s)?(?:[a-z0-9-_]+\s?=)?\s?(?:[a-z0-9]+\s+\:\s+)?(?:function\s?)?(?:[a-z0-9_-]+)?\s?\(.*\)\s?(?:.+)?([=>]:)?\{(?:(?:[^}{]+|\{(?:[^}{]+|\{[^}{]*\})*\})*\}(?:\s?\(.*\)\s?\)\s?)?)?(?:\;)?

, : https://regex101.com/r/WGqfm8/9/

, !

, :

// won't find
const filterFn = apples.filter(() => {});
// won't find
const filterFn = apples.filter(function(){});

-, , , .

0

All Articles