Garry S
Garry S

Reputation: 117

Get function names from specific string

I have string with slash separated contains function names.

e.g.
my_doc/desktop/customer=getCustomer()/getCsvFileName()/controller=getControllerName()

Within above string I want only function name i.e. getCustomer(), getControllerName() & getCsvFileName()

I searched some regex like:

let res = myString.match(/(?<=(function\s))(\w+)/g);

but its returning result as null.

Update: Now I want to get function names without parentheses () i.e. getCustomer, getControllerName & getCsvFileName

Please help me in this

Upvotes: 2

Views: 85

Answers (3)

human bean
human bean

Reputation: 857

const str = "my_doc/desktop/customer=getCustomer()/getCsvFileName()/controller=getControllerName()"

let tokens = [];

for (element of str.split("/"))
  if (element.endsWith("()"))
    tokens.push(element.split("=")[1] ?? element.split("=")[0])

console.log(tokens);

General idea: split the string along slashes, and for each of these tokens, if the token ends with () (as per Nick's suggestion), split the token along =. Append the second index of the token split along = if it exists, otherwise append the first.

A "smaller" version (using purely array methods) could be:

const str = "my_doc/desktop/customer=getCustomer()/getCsvFileName()/controller=getControllerName()"

let tokens = str.split("/")
                .filter(element => element.endsWith("()"))
                .map(element => element.split("=")[1] ?? element.split("=")[0]);

console.log(tokens);

Upvotes: 1

Soorya J
Soorya J

Reputation: 145

As per, MDN docs the match() method returns null if it does not find a match for the provided regex in the provided search string.

The regular expression which you have provided,/(?<=(function\s))(\w+)/g matches any word that has 'function ' before it. (NOTE: a space after the word function)

Your search string my_doc/desktop/customer=getCustomer()/getCsvFileName()/controller=getControllerName() does not include 'function ' before any characters. That is why you got null as result of match() method.

let yourString = 'my_doc/desktop/customer=getCustomer()/getCsvFileName()/controller=getControllerName()';

let myReferenceString = 'SAMPLETEXTfunction sayHi()/function sayHello()';


let res = yourString.match(/(?<=(function\s))(\w+)/g);
let res2 = myReferenceString.match(/(?<=(function\s))(\w+)/g);

console.log("Result of your string", res);
console.log("Result of my string", res2);

My solution here,

let myreferenceString = 'my_doc/desktop/customer=getCustomer()/getCsvFileName()/controller=getControllerName()'

let res = myreferenceString.match(/((?<==)(\w+\(\)))|((?<=\/)(\w+\(\)))/g);

console.log("Result", res);

NOTE: I have used the 'Positive Look Behind regex operator', This is not supported in browsers like Safari and IE. Please do reasearch about this before considering this approach.

Upvotes: 0

Maniraj Murugan
Maniraj Murugan

Reputation: 9084

You can split the string that has parentheses () first like /.*?\([^)]*\)/g.

This will give array of results, and after that you can iterate the array data and for each item, you can split the = and / before function name with the help of item.split(/=|\//).

Then push the filtered function name into empty array functionNames.

Working Example:

const string = `my_doc/desktop/customer=getCustomer()/getCsvFileName()/controller=getControllerName()`;

const functionNames = [];

string.match(/.*?\([^)]*\)/g).forEach(item => {
  const splitString = item.split(/=|\//);
  const functionName = splitString[splitString.length - 1];
  functionNames.push(functionName);
});

console.log(functionNames);

Upvotes: 1

Related Questions