The method for calling regular expressions in JavaScript is provided with code. Once ECMAScript4 suggested that this feature would be added to the ES4 specification, but later in the ES4-discussmailinglist discussions, this suggestion could be abolished. However, you can point out that this feature will be added to the ES4 specification by adding previously ECMAScript 4, but later in the discussion of the ES4-discuss mailing list, this suggestion may be abolished.
However, you can add the call and apply methods to the similar methods in RegExp. prototype. It not only facilitates function design, but also implements the hidden type (duck-typed) code that is valid for both functions and regular expressions. Therefore, let's add these methods.
RegExp. prototype. call = function (context, str ){
Return this.exe c (str );
};
RegExp. prototype. apply = function (context, args ){
Return this.exe c (args [0]);
}; Note that the preceding two methods completely ignore the context parameter. you can submit null or any other object serving as the context, and you will get the return value of the regular exec method similarly. Using the above method makes it much easier to use regular expressions and functions under any circumstances. Some obvious examples are useful in array iteration of JavaScript 1.6. The following filter, every, some, and map methods can be executed across browsers.
If (! Array. prototype. filter ){
// Returns an array. if the provided filter function returns true, it returns the elements in the existing array.
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;
};
}
If (! Array. prototype. every ){
// Returns true if each element in the array meets 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;
};
}
If (! Array. prototype. some ){
// Return true if at least one element in the array meets 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. The return value of the function provided by each element in the existing 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;
};
} Because the exec method returns an array or null value and converts the appropriate types to true and false, the code above allows us to use ["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 invocation of exec has played a role in this browser. However, if the filter does not add the RegExp. prototype. call method, it cannot be executed across browsers.
Bytes. However, you can add...