1

I'm new to Regex, took some tutorials, having a hard time getting this right.

I'm working on a language support for TypeScript for a text editor, and I need a regex that matches JUST the typing information in a function. For example:

function asdf(param1:type1, param2:type2, param3:type3) {...}

In that, my regex should match 'type1', 'type2', 'type3' etc.

Here's what I'm trying:

\(.+?:(\w).+?\)

Breaks down like this:

\( look for starting parenthesis

.+?: any number of characters up to the colon

(\w) capture group: the next word

.+? There may be additional words after this

\) Close parentheses for the end of the function parameters.

Not sure what I'm doing wrong, but like I said I'm new to regex. Currently it captures the entire stuff, from beginning parens to closing parens. I need it to match JUST the single words after the colon.

NOTE: I want to make sure it only matches words after colons inside parens, so that it doesn't match object key/data combos, which look similar in Javascript.

Thanks!

isaachess
  • 739
  • 1
  • 7
  • 15
  • What is the regex engine you use? – Casimir et Hippolyte Oct 10 '14 at 14:09
  • This is going to be for the Atom text editor, so I assume it's Javascript in the end since it is built using web technologies. – isaachess Oct 10 '14 at 14:15
  • A more general way to ask this question is: how do I include characters in a regex query without having them be PART of the match? If I want words after a colon (:) for example, can I include the colon without matching the colon somehow? – isaachess Oct 10 '14 at 14:16
  • The javascript regex engine is not really able to do something like that. you can cheat with this kind of pattern: `[^:)]+(?=,[^()]*\)|\))` but note that this doesn't check the opening parenthesis. – Casimir et Hippolyte Oct 10 '14 at 14:23
  • with a more advanced regex engine (perl/php/java/.net) you can write: `(?:\G(?!\A), |\()[^:]+:\K[^,)]+(?=[^()]*\))` that is more robust. – Casimir et Hippolyte Oct 10 '14 at 14:27
  • Regex is not really powerful enough to parse something like this. TypeScript is open source and has a parser that is guaranteed to produce the correct result; why not use that? – Ryan Cavanaugh Oct 10 '14 at 16:17
  • I'm trying to update and contribute to a Typescript Language package for Atom. The sepcs say that you include regex for what should be matched, then specify which classes to attach to the match. I believe I can only use raw regex, which is why I'm looking for a regex solution. – isaachess Oct 10 '14 at 17:12
  • But perhaps I'm misunderstanding and there's still a better way? I'd love to hear it. :) – isaachess Oct 10 '14 at 17:12

1 Answers1

2

Try thus

var STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg;
var ARGUMENT_NAMES = /([^\s,]+)/g;
function getParamNames(func) {
  var fnStr = func.toString().replace(STRIP_COMMENTS, '')
  var result = fnStr.slice(fnStr.indexOf('(')+1, fnStr.indexOf(')')).match(ARGUMENT_NAMES)
  if(result === null)
     result = []
  for(i=0; i< result.length; i++)
      result[i]=result[i].split(":")[1];
  return result
}

how to use

   getParamNames("function asdf(param1:type1, param2:type2, param3:type3) {...}");

reference

How to get function parameter names/values dynamically from javascript

Community
  • 1
  • 1
Fabio
  • 1,890
  • 1
  • 15
  • 19