2

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

3 Answers 3

1

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);

Sign up to request clarification or add additional context in comments.

3 Comments

.endsWith() might make more sense than taking a substring.
@human-bean your code is working perfectly fine. Can I get function names without brackets ? e.g. getCustomer, getControllerName, getCsvFileName
Yes. Simply change the expressions element.split("=")[1] and element.split("=")[0] to element.split("=")[1].slice(0, -2) and element.split("=")[0].slice(0, -2), respectively, to remove the trailing brackets for each element added to the array.
1

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);

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.