Javascript space removal method)

Source: Internet
Author: User

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?
  1. Function array_max ()
  2. {
  3. VaR I, max = This [0];
  4. For (I = 1; I <this. length; I ++)
  5. {
  6. If (max <this [I])
  7. Max = This [I];
  8. }
  9. Return Max;
  10. }
  11. Array. Prototype. max = array_max;
  12. VaR x = new array (1, 2, 3, 4, 5, 6 );
  13. 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?
  1. <Script language = "JavaScript">
  2. // Remove spaces on both sides
  3. String. Prototype. Trim = function ()
  4. {
  5. Return this. Replace (/(^ \ s *) | (\ s * $)/g ,"");
  6. }
  7. // Remove the left space
  8. String. Prototype. ltrim = function ()
  9. {
  10. Return this. Replace (/(^ \ s *)/g ,"");
  11. }
  12. // Remove right space
  13. String. Prototype. rtrim = function ()
  14. {
  15. Return this. Replace (/(\ s * $)/g ,"");
  16. }
  17. </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.

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.