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);
分享到:
相关推荐
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 ...
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 ...
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 ...
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 ...
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...
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 begins with fundamentals--variables, control structures, functions, and data structures--then moves on to complex topics like object-oriented programming and regular expressions....
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...
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中,可以利用System.Text.RegularExpressions命名空间中的Regex类来实现这一功能。 首先,我们需要引入必要的命名空间: ```vbnet Imports System.Text.RegularExpressions ``` 接下来,创建一个函数,该...
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 ...
- **Regular Expressions:** Brief introduction to regular expressions for advanced string processing. **Chapter 6: Arrays** - **Array Basics:** Explanation of arrays in PHP, including indexed arrays ...
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, ...
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 ...