Remove whitespace from a string

Source: Internet
Author: User

What is Q:string.prototype.trim for?

A: Add a trim method to the string object.

1 <!DOCTYPE HTML>2 <HTMLLang= "en">3 <Head>4     <MetaCharSet= "UTF-8">5     <title></title>6     <styletype= "Text/css">7 . Text_page{Line-height:28px;}8     </style>9 Ten </Head> One <Body> A <Divclass= "Text_page"> -     <b>/(^\s*) | (\s*$) Explanation of/g:</b> -^ indicates that the string must begin with a subsequent rule, where the string must begin with \s*.<BR> the\s is the meaning of a space, * indicates that there are 0 or more<BR> -\s* is 0 or more spaces<BR> -(^\s*) means starting with 0 spaces or multiple spaces<BR> -| To express or mean to satisfy | The left is also set up to meet | The right side is also set up.<BR> +\s* said it earlier.<BR> -The $ means that the string must end with the preceding rule<BR> +(\s*$) means to end with 0 spaces or a number of spaces<BR> A/.../g is a property of a regular expression that represents a full-text match, rather than a stop to find one.<BR> atSo this regular expression replaces the leading and trailing spaces.<BR> -Because there are leading spaces, it is stated that 0 or more spaces begin with a regular expression Previous conditions: (^\s*)<BR> -And if there is a trailing space, it means ending with 0 or more spaces, satisfying the regular expression | The following conditions: (\s*$)<BR> - </Div> - </Body> - </HTML> in <Scripttype= "Text/javascript"> -     /** to * Here is the definition + * @description: Remove spaces on both sides of the string - * @type: String.prototype.trim the      */ * String.prototype.trim= function() { $         return  This. Replace (/(^\s*) | (\s*$)/G,"");Panax Notoginseng     } -  the     varStr= "mycodeing"; + Str=Str.trim ();//S is a string that can use the trim method you just defined.  A Console.log (str); the </Script>

Remove whitespace from a string

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.