`
鹤惊昆仑
  • 浏览: 229036 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Regular Expressions As Functions

阅读更多
see 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);

分享到:
评论

相关推荐

    Oracle正则表达式参考手册

    It's suitable for those who have never used regular expressions before, as well as those who have experience with Perl and other languages supporting regular expressions. The book describes Oracle ...

    Jeffrey E. F. Friedl - Mastering.Regular.Expressions.3rd.Edition

    If you think you know all you need to know about regularexpressions, this book is a stunning eye-opener. As this book shows, a command of regular expressions is an invaluable skill. Regular ...

    Perl-compatible Regular Expressions VC compile project

    The PCRE library is a set of functions that implement regular expression pattern matching using the same syntax and semantics as Perl, with just a few differences. Certain features that appeared in ...

    Data.Collection.with.R.A.Practical.Guide.to.Web.Scraping.and.Text.Mining

    Chapter 8: Regular expressions and essential string functions Part Two: A Practical Toolbox for Web Scraping and Text Mining Chapter 9: Scraping the Web Chapter 10: Statistical text processing ...

    Modern C++ Programming Cookbook

    You will learn about concepts such as concurrency, performance, meta-programming, lambda expressions, regular expressions, testing, and many more in the form of recipes. These recipes will ensure you...

    Python Regular Expression

    This module exports the following functions: match Match a regular expression pattern to the beginning of a string. search Search a string for the presence of a pattern. sub Substitute occurrences ...

    Eloquent Javascript, 3rd Edition

    Eloquent JavaScript begins with fundamentals--variables, control structures, functions, and data structures--then moves on to complex topics like object-oriented programming and regular expressions....

    sed-awk-2nd-edition.chm

    Because all three programs use UNIX regular expressions, an entire chapter is devoted to understanding UNIX regular expression syntax. Next, the book describes how to write sed scripts. After getting...

    article-regex-primer.rar_The Few

    Java Regex Primer Since version 1.4, Java has had support for Regular Expressions in the core API. Java Regex follows the same basic principles used in other languages, just withdi erent access ...

    vb.net例子-正则-取函数

    在VB.NET中,可以利用System.Text.RegularExpressions命名空间中的Regex类来实现这一功能。 首先,我们需要引入必要的命名空间: ```vbnet Imports System.Text.RegularExpressions ``` 接下来,创建一个函数,该...

    PHP.and.MySQL.Recipes.A.Problem-Solution.Approach.1484206061

    Chapter 9: Regular Expressions Chapter 10: Variables Chapter 11: Functions Chapter 12: Web Fundamentals Chapter 13: Creating and Using Forms Chapter 14: XML, RSS, WDDX, and SOAP Chapter 15: Data ...

    Beginning PHP 5.3

    - **Regular Expressions:** Brief introduction to regular expressions for advanced string processing. **Chapter 6: Arrays** - **Array Basics:** Explanation of arrays in PHP, including indexed arrays ...

    Core PYTHON Programming, 2nd Edition (epub 格式, Python 2.5)

    After you learn the core fundamentals of Python, he shows you what you can do with your new skills, delving into advanced topics, such as regular expressions, networking programming with sockets, ...

    scala for the impatient

    and more * Becoming familiar with object-oriented programming in Scala: classes, inheritance, and traits * Using Scala for real-world programming tasks: working with files, regular expressions, and ...

Global site tag (gtag.js) - Google Analytics