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.
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.
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.
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");
}