This project is archived and is in readonly mode.

#43 ✓invalid
Thomas Aylott

RegExp as a Function

Reported by Thomas Aylott | May 28th, 2008 @ 10:05 AM | in 2.0 (closed)

Originally posted on Trac by mattcoz

Original Trac Ticket

Description

Got this great idea from this blog:

http://blog.stevenlevithan.com/a...

In Firefox you can call a regular expression as a function, which can be very useful in the Array map, filter, every, and some functions. Basically you could do something like this:

['cat','dog'].filter(/cat/) ==> ['cat']

or

['cat','dog'].every(/cat/) ==> false

He has better examples on his blog post.

It is my suggestion to add this capability to the RegExp? native so it can be used cross-browser.

Index: trunk/Source/Native/RegExp.js
===================================================================
--- trunk/Source/Native/RegExp.js	(revision 0)
+++ trunk/Source/Native/RegExp.js	(revision 0)
@@ -0,0 +1,19 @@
+/*
+Script: RegExp.js
+	Contains RegExp Prototypes for use as a Function.
+
+License:
+	MIT-style license.
+*/
+
+RegExp.implement({
+
+	call: function(bind, str){
+		return this.exec(str);
+	},
+	
+	apply: function(bind, args){
+		return this.apply(args);
+	}
+
+});

Trac Attachments

Trac Comments

Changed 2 weeks ago by tomocchino


        
we may add a RegExp? native after the release of 1.2 

                  Changed 2 weeks ago by tomocchino

          milestone
              changed from Mootools version 1.2 to Mootools version 1.3

we may add a RegExp? native after the release of 1.2 

          milestone
              changed from Mootools version 1.2 to Mootools version 1.3
        
      

Changed 2 weeks ago by tomocchino



                  Changed 2 weeks ago by tomocchino

          milestone
              set to Mootools version 1.3

          milestone
              set to Mootools version 1.3
        
      

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

Shared Ticket Bins

Pages