Regular Expressions As Functions

see [url]http://blog.stevenlevithan.com/archives/regular-expressions-as-functions[/url]
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);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值