JS functions call Regular Expressions

Source: Internet
Author: User

This provides convenience for calling the regular exec method. For example, in Firefox, RegEx ("string") is equivalent to regex.exe C ("string "). Once ecmascript 4 suggested that this feature would be added to the ES4 specification, but later in the discussion of the ES4-discuss mailing list, this suggestion could be abolished.

However, you can add the call and apply methods to the similar methods in Regexp. prototype. It is helpful for function design, and can implement a hidden type (duck-typed) that is valid for both functions and regular expressions)Code. Therefore, let's add these methods.

Copy code The Code is as follows: 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 obtain 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.

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;
};
}

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.