Reglar Expressions As Functions
see
http://blog.stevenlevithan.com/archives/regular-expressions-as-functions
http://blog.stevenlevithan.com/archives/regular-expressions-as-functions
RegExp.prototype.call = function (context, str) {
return this.exec(str);
};
RegExp.prototype.apply = function (context, args) {
return this.exec(args[0]);
};
// Returns an array with the elements of an existng array for which the provided filtering function returns true
Array.prototype.filter = function (func, context) {
var results = [];
for (var i = 0; i < this.length; i++) {
if (i in this && func.call(context, this[i], i, this))
results.push(this[i]);
}
return results;
};
// Returns true if every element in the array satisfies the provided testing function
Array.prototype.every = function (func, context) {
for (var i = 0; i < this.length; i++) {
if (i in this && !func.call(context, this[i], i, this))
return false;
}
return true;
};
// Returns true if at least one element in the array satisfies the provided testing function
Array.prototype.some = function (func, context) {
for (var i = 0; i < this.length; i++) {
if (i in this && func.call(context, this[i], i, this))
return true;
}
return false;
};
// Returns an array with the results of calling the provided function on every element in the provided array
Array.prototype.map = function (func, context) {
var results = [];
for (var i = 0; i < this.length; i++) {
if (i in this)
results[i] = func.call(context, this[i], i, this);
}
return results;
};
var a=["a","b","ab","ba"].filter(/^a/);
alert(a);
if(window.console && window.console.log)console.log(a);