js下函数般调用正则的方法附代码_PHP
曾经 ECMAScript 4 建议指出这个功能将会增加到 ES4 规范中,但后来的在 ES4-discuss mailing list 的讨论中,这个建议可能被废除。
然而,你可以通过增加 call 和 apply 方法到 RegExp.prototype 中类似的现实这些方法。既有助于功能设计,又可实现对函数和正则表达式均有效的隐藏类型(duck-typed )代码。因此,让我们增加这些方法。
RegExp.prototype.call = function (context, str) {
return this.exec(str);
};
RegExp.prototype.apply = function (context, args) {
return this.exec(args[0]);
};注意上面的两个方法完全忽略 context 参数,你可以提交 null 或者 任何其他作为 context 的对象,并且你将会类似的得到正则 exec 方法的返回值。使用上面的方法,无论在什么情况下,使我们正常地使用正则表达式和函数变得容易得多。一些很明显的例子,比如这些在 JavaScript 1.6 的数组迭代中很有用。下面的 filter, every, some, 和 map 方法的执行可以跨浏览器。
if (!Array.prototype.filter) {
// 返回一个数组,如果提供的过滤函数返回 true,则返回存在的数组中的元素。
Array.prototype.filter = function (func, context) {
var results = [];
for (var i = 0; i if (i in this && func.call(context, this[i], i, this))
results.push(this[i]);
}
return results;
};
}
if (!Array.prototype.every) {
// 返回 true ,如果数组中的每个元素满足提供的测试函数。
Array.prototype.every = function (func, context) {
for (var i = 0; i if (i in this && !func.call(context, this[i], i, this))
return false;
}
return true;
};
}
if (!Array.prototype.some) {
// 返回 true,如果数组中至少有一个元素满足提供的测试函数。
Array.prototype.some = function (func, context) {
for (var i = 0; i if (i in this && func.call(context, this[i], i, this))
return true;
}
return false;
};
}
if (!Array.prototype.map) {
// 返回一个数组,现有数组中的每个元素调用提供的函数的返回值。
Array.prototype.map = function (func, context) {
var results = [];
for (var i = 0; i if (i in this)
results[i] = func.call(context, this[i], i, this);
}
return results;
};
}因为exec 方法返回数组或 null 值,并会恰当的类型转换为 true 和 false,上面的代码允许我们像这样使用:["a","b","ab","ba"].filter(/^a/),返回所有以“a”开始的值:["a","ab"]。
确实,在 Firefox 中已经实现了 Array.prototype.filter ,由于 exec 的间接调用已经在该浏览器中起作用了。但是如果 filter 没有添加 RegExp.prototype.call 方法,却无法跨浏览器执行。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Go language provides two dynamic function creation technologies: closure and reflection. closures allow access to variables within the closure scope, and reflection can create new functions using the FuncOf function. These technologies are useful in customizing HTTP routers, implementing highly customizable systems, and building pluggable components.

In C++ function naming, it is crucial to consider parameter order to improve readability, reduce errors, and facilitate refactoring. Common parameter order conventions include: action-object, object-action, semantic meaning, and standard library compliance. The optimal order depends on the purpose of the function, parameter types, potential confusion, and language conventions.

The key to writing efficient and maintainable Java functions is: keep it simple. Use meaningful naming. Handle special situations. Use appropriate visibility.

1. The SUM function is used to sum the numbers in a column or a group of cells, for example: =SUM(A1:J10). 2. The AVERAGE function is used to calculate the average of the numbers in a column or a group of cells, for example: =AVERAGE(A1:A10). 3. COUNT function, used to count the number of numbers or text in a column or a group of cells, for example: =COUNT(A1:A10) 4. IF function, used to make logical judgments based on specified conditions and return the corresponding result.

Golang regular expressions use the pipe character | to match multiple words or strings, separating each option as a logical OR expression. For example: matches "fox" or "dog": fox|dog matches "quick", "brown" or "lazy": (quick|brown|lazy) matches "Go", "Python" or "Java": Go|Python |Java matches words or 4-digit zip codes: ([a-zA

The advantages of default parameters in C++ functions include simplifying calls, enhancing readability, and avoiding errors. The disadvantages are limited flexibility and naming restrictions. Advantages of variadic parameters include unlimited flexibility and dynamic binding. Disadvantages include greater complexity, implicit type conversions, and difficulty in debugging.

The benefits of functions returning reference types in C++ include: Performance improvements: Passing by reference avoids object copying, thus saving memory and time. Direct modification: The caller can directly modify the returned reference object without reassigning it. Code simplicity: Passing by reference simplifies the code and requires no additional assignment operations.

The difference between custom PHP functions and predefined functions is: Scope: Custom functions are limited to the scope of their definition, while predefined functions are accessible throughout the script. How to define: Custom functions are defined using the function keyword, while predefined functions are defined by the PHP kernel. Parameter passing: Custom functions receive parameters, while predefined functions may not require parameters. Extensibility: Custom functions can be created as needed, while predefined functions are built-in and cannot be modified.
