Native JS-remove null characters at the beginning and end of a string

Source: Internet
Author: User

How to solve this problem??

The idea is that we use the regular match to the so-called space, and then replace the null character, we need to use the STR's replace API

The code is as follows:

1 <!DOCTYPE HTML>2 <HTMLLang= "en">3 <Head>4     <MetaCharSet= "UTF-8">5     <title>Document</title>6 </Head>7 <Body>8     <Script>9         //remove spaces at the beginning and end of a stringTen         functiondelate (str) { One             //match the space to the beginning, and then replace it with a blank, returning a new string str1 A Let str1=Str.replace (/\s+$/,"');  -             //back to str2 -              the Let str2=Str1.replace (/^\s+/,"') -             returnstr2; -         } - Console.log (Delate ('text')); + Console.log ('text'); -     </Script> + </Body> A </HTML>

Note: Replace is replaced by the previous one, and then the null character is replaced with \s, and then with what starts with ^.

Native JS-remove null characters at the beginning and end of 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.