Home > Web Front-end > JS Tutorial > body text

Calling regex like js function_javascript skills

WBOY
Release: 2016-05-16 19:05:25
Original
1043 people have browsed it

This provides convenience for calling the regular exec method. For example, in Firefox, regex("string") is equivalent to regex.exec("string"). There was an ECMAScript 4 suggestion that this feature would be added to the ES4 specification, but later in the ES4-discuss mailing list discussion, this suggestion may have been scrapped.

However, you can implement these methods similarly by adding the call and apply methods to RegExp.prototype. It not only helps in functional design, but also enables the implementation of duck-typed code that is valid for both functions and regular expressions. So let's add these methods.

Copy code The code is as follows:

RegExp.prototype.call = function (context, str) {
return this.exec(str);
};
RegExp.prototype.apply = function (context, args) {
return this.exec(args[0]);
};

Note that the above two methods completely ignore the context parameter, you can submit null or any other object as context, and you will get the return value of the regular exec method similarly. Using the above method, it becomes much easier to use regular expressions and functions normally no matter what the situation. Some obvious examples, such as these are useful for array iteration in JavaScript 1.6. The following filter, every, some, and The >map method can be executed across browsers.

Copy code The code is as follows:

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. prototype.every) {
// Return true if each element in the array satisfies the provided test 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;
return true;
if (!Array.prototype. some) {
// Returns true if at least one element in the array satisfies the provided test 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;
};
}

if (!Array.prototype.map ) {
// Returns an array. Each element in the existing array calls the return value of the provided function.
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;
};
}


Because the exec method returns an array or null value, with appropriate type conversions to true and false, the above code allows us to use it like this: ["a","b","ab","ba "].filter(/^a/), returns all values ​​starting with "a": ["a","ab"].

Indeed, Array.prototype.filter has been implemented in Firefox, because the indirect call of exec already works in this browser. However, if the filter does not add the RegExp.prototype.call method, it cannot be executed across browsers.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template