0

I need a little help using regex.

This is the scenario :

function name(a :String)

or

function name(a :String) : String

If we have the version with return type, I will need exactly the string after the colon, otherwise I would need let's say an empty string.

Thanks in advance.

Aditi
  • 1,188
  • 2
  • 16
  • 44
Olaru Mircea
  • 2,570
  • 26
  • 49

4 Answers4

0

Try this :

[a-zA-Z]*\s*\)\s*:\s*([a-zA-Z]*)
Aditi
  • 1,188
  • 2
  • 16
  • 44
0

This regex may help:

\)\W+(\w+)

where $1 will hold the value of return type

Aziz Shaikh
  • 16,245
  • 11
  • 62
  • 79
0

Any word followed by : and a whitespace

(?<=:\s)(\w+)
Evelie
  • 2,919
  • 2
  • 14
  • 21
0

Try the following:

matches = /\)\s*:\s*(\w*)/gi.exec("function name(a :String) : String");
retType = matches && matches.length>=2 ? matches[1] : "";
Garfield
  • 2,487
  • 4
  • 31
  • 54