JS removes whitespace with the replace () method

Source: Internet
Author: User

<script>

Remove all spaces

var str = "546546 4564 46 46 88 88";

var str = str.replace (/\s+/g, "");//generates a new string and does not modify the original string

Console.log (str);

Remove left space

var str1 = "546546 4564 46 46 88 88";

var str1 = Str1.replace (/^\s*/, "");

Console.log (STR1);

Remove Right space

var str2 = "546546 4564 46 46 88 88";

var str2 = str2.replace (/(\s*$)/g, "");

Console.log (STR2);

Remove left and right spaces

var str3 = "546546 4564 46 46 88 88";

var str3 = Str3.replace (/^\s+|\s+$/g, "");

Console.log (STR3)

</script>

JS removes whitespace with the replace () method

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.