0

I have a string containing expressions such as calling to functions and properties:

$ctrl.accounts $ctrl.fn() $ctrl.fns(arg) $ctrl.foo.bar $ctrl.bay.bag() $ctrl.bar.fn(arg)

I made this regex that matches the word after $ctrl.: /(?<=\$ctrl\.)[a-zA-Z]+(?=\s|\.|\(|$)/g. This matches the property or the function without . or (. (?=\s|\.|\(|$).

The output of this string against this regex would be:

"accounts", "fn", "fns", "foo", "bay", "bar"

That's working as expected:

const input = `$ctrl.accounts $ctrl.fn() $ctrl.fns(arg) $ctrl.foo.bar $ctrl.bay.bag() $ctrl.bar.fn(arg)`
const results = input.match(/(?<=\$ctrl\.)[a-zA-Z]+(?=\s|\.|\(|$)/g);
console.log({ results });

But now I try to match only the function calls: fn, fns.

I removed \s|\. from (?=\s|\.|\(|$) but it doesn't work:

/(?<=\$ctrl\.)[a-zA-Z]+(?=\(|$)/g

For this input: account by $ctrl.accounts, it matches accounts but it shouldn't.

How I change that regex to match only functions calling, but only the function after $ctrl.******(?

1 Answer 1

1

You must remove the following |, then it will match as you wish. So remove \s|\.| from 1st regex.

const input = `$ctrl.accounts $ctrl.fn() $ctrl.fns(arg) $ctrl.foo.bar $ctrl.bay.bag() $ctrl.bar.fn(arg)`
const results = input.match(/(?<=\$ctrl\.)[a-zA-Z]+(?=\(|$)/g);;
console.log({
  results
})

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

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.