views:

134

answers:

3

Having some problems figuring out the regex to match this:

function Array() { [native code] }

I'm trying to only match the text that will occur where "Array" is.

+3  A: 

In Perl, you'd use:

m/^\s*function\s+(\w+)\s*\(/;

The variable '$1' would capture the function name.

If the function keyword might not be at the start of the line, then you have to work (a little) harder.

[Edit: two '\s*' sequences added.]


Question about whether this works...here's my test case:

Test script:

while (<>)
{
    print "$1\n" if (m/^\s*function\s+(\w+)\s*\(/);
}

Test input lines (yes, deliberately misaligned):

function Array() { ... }
 function   Array2   () { ... }
func Array(22) { ... }

Test output:

Array
Array2

Tested with Perl 5.10.0 on Solaris 10 (SPARC): I don't believe the platform or version is a significant factor - I'd expect it to work the same on any plausible version of Perl.

Jonathan Leffler
Is it that hard to add a \s* to the beginning? And might it not be a bad idea (though I don't know JavaScript) to add a \s* between the (\w+) and the \( ?
Chris Lutz
@Chris: +1. I opened up the comments box to suggest the same...
Swanand
Since everybody is nitpicking already ;) Why including the ^ and the \s* at all? There is no need for this constraint, a simple \bfunction to indicate a word boundary should do it. The \s* at the end is also unneeded, why care at all what follows the match?
Tomalak
The caret avoids problems with function appearing in strings or comments or... It is not perfect. The \s* at the end allows spaces after the name and before the open parenthesis. If there's no risk of ambiguity ('function Ambiguous works'), then OK. The problem scope is not completely clear.
Jonathan Leffler
I've been trying this, and not getting it working. It still matches 'function'.
Geuis
@Tomalak - I wouldn't want the regex to match "function Bad Code()", where someone accidentally wrote a function name with a space. It should look for the parenthesis to make sure the function at least has decent syntax. This is just my opinion, though.
Chris Lutz
+4  A: 

Are you trying to find out what type a variable is in javascript? If that's what want you can just compare the object's constructor to the constructor that you think created it:

var array = new Array();
if(array.constructor == Array)
    alert("Is an array");
else
    alert("isn't an array");

This isn't really the best way to go about things in javascript. Javascript doesn't have a type system like C# does that guarantees you that a variable will have certain members if it's created by a certain constructor because javascript is a pretty dynamic languages and anything that an object gets from its constructor can be overwritten at runtime.

Instead it's really better to use duck typing and ask your objects what they can do rather than what they are: http://en.wikipedia.org/wiki/Duck_typing

if(typeof(array.push) != "undefined")
{
    // do something with length
    alert("can push items onto variable");
}
Helephant
This actually helped me solve the problem I was attempting. Thank you for reading through what I was asking to seeing my intention.
Geuis
+1 for psychic debugging skills. :-)
Tomalak
A: 

So subsequently, I've gotten this which almost works:

[function\s]((\S+)+)(?=\(\))

However, it still matches the space before Array

Geuis
Remove the `[`, `]`.
Gumbo