Http://blog.csdn.net/cyanapple_wen/article/details/5420949
In my opinion, the best method is to use regular expressions, which is the core principle.
Secondly, this method uses the prototype attribute of JavaScript.
In fact, you can use functions without using this attribute, but it is more convenient to use it later.
Next let's take a look at how this attribute is used.
Returns a reference to an object type prototype.
Objectname. Prototype
The objectname parameter is the object name.
Description
The prototype attribute provides a set of basic functions of the object class. The new instance of the object "inherits" the operation that is granted to the object prototype.
For example, you want to add a method to the array object to return the maximum element value in the array. To accomplish this, declare the function, add it to array. prototype, and use it.
[JavaScript]View plaincopyprint?
- Function array_max ()
- {
- VaR I, max = This [0];
- For (I = 1; I <this. length; I ++)
- {
- If (max <this [I])
- Max = This [I];
- }
- Return Max;
- }
- Array. Prototype. max = array_max;
- VaR x = new array (1, 2, 3, 4, 5, 6 );
- Var y = x. Max ();
Function array_max () <br/>{< br/> var I, max = This [0]; <br/> for (I = 1; I <this. length; I ++) <br/>{< br/> If (max <this [I]) <br/> max = This [I]; <br/>}< br/> return Max; <br/>}< br/> array. prototype. max = array_max; <br/> var x = new array (1, 2, 3, 4, 5, 6); <br/> var y = x. max ();
After the code is executed, y saves the maximum value in array X, or 6.
All internal JScript objects have the read-only prototype attribute. You can add features for the prototype as in this example, but the object cannot be assigned different prototypes. However, user-defined objects can be assigned to new prototypes.
The method and attribute list of each internal object in this language reference specifies which parts of the object prototype and which parts are not.
Below is the original code
Program code
[JavaScript]View plaincopyprint?
- <Script language = "JavaScript">
- // Remove spaces on both sides
- String. Prototype. Trim = function ()
- {
- Return this. Replace (/(^ \ s *) | (\ s * $)/g ,"");
- }
- // Remove the left space
- String. Prototype. ltrim = function ()
- {
- Return this. Replace (/(^ \ s *)/g ,"");
- }
- // Remove right space
- String. Prototype. rtrim = function ()
- {
- Return this. Replace (/(\ s * $)/g ,"");
- }
- </SCRIPT>
<Script language = "JavaScript"> <br/> // remove spaces on both sides <br/> string. prototype. trim = function () <br/>{< br/> return this. replace (/(^/S *) | (/S * $)/g ,""); <br/>}< br/> // remove left space <br/> string. prototype. ltrim = function () <br/>{< br/> return this. replace (/(^/S *)/g, ""); <br/>}< br/> // remove right space <br/> string. prototype. rtrim = function () <br/>{< br/> return this. replace (/S * $)/g, ""); <br/>}< br/> </SCRIPT>Next let's take a look at what \ s represents in JavaScript scripts"
\ S matches any blank characters, including spaces, tabs, and page breaks. It is equivalent to [\ f \ n \ r \ t \ v]. Note that the lower-case s \ s matches any non-blank characters. It is equivalent to [^ \ f \ n \ r \ t \ v]. Global attributes
Returns a Boolean value indicating the status of the global flag used by the regular expression. The default value isFalse. Read-only.