Reputation: 559
this is my code
var javascriptCode="function adding(a,b){ return a+b}"
and i want to pick functionName adding
from that string how to do that?
i have tried by finding its index as
var indexStart=javascriptCode.indexOf("function ");
var indexEnd=javascriptCode.indexOf("(");
var res = javascriptCode.substring(indexStart, indexEnd);
but i am getting the output as
function adding
, is their any other way to do this
Upvotes: 0
Views: 33
Reputation: 459
You could use regular expression.
var javascriptCode="function adding(a,b){ return a+b}"
var regex = /function\s*([^\(]*)\(/
var matchResult = javascriptCode.match(regex)
var funcName = null;
if (matchResult && matchResult.length) {
funcName = matchResult[1];
}
console.log(funcName);
Upvotes: 1
Reputation: 25352
Try like this
var javascriptCode="function adding(a,b){ return a+b}";
var indexStart=javascriptCode.indexOf(" ");
var indexEnd=javascriptCode.indexOf("(");
var res = javascriptCode.substring(indexStart+1, indexEnd);
console.log(res);
Upvotes: 1