JavaScript remove before and after space program code

Source: Internet
Author: User
Tags rtrim

Code

The code is as follows Copy Code

function String.prototype.Trim () {return this.replace (/^/s*) | (   /s*$)/g, ""); Remove left and right spaces

function String.prototype.Ltrim () {return this.replace (/(^/s*)/g, "");} Remove left space

function String.prototype.Rtrim () {return this.replace (/(/s*$)/g, "");} Remove Right space

Cases

The code is as follows Copy Code

<script type= "Text/javascript" >
function Trim (str) {//Remove spaces at left and right ends
Return Str.replace (/(^s*) | ( s*$)/g, "");
}
function LTrim (str) {//delete left space
Return Str.replace (/(^s*)/g, "");
}
function RTrim (str) {//Remove the right space
Return Str.replace (/(s*$)/g, "");
}
</script>

example, remove all spaces

Program code

  code is as follows copy code

<script Language= "JavaScript"
<!--
//provenance: Online collection
//made by Yaosansi 2005-12-02
//for more visit http:// Www.yaosansi.com
//Trim (), Ltrim (), RTrim ()

String.prototype.Trim = function ()
{
Return This.repla CE (/(^s*) | ( s*$)/g, "");
}

String.prototype.LTrim = function ()
{
return This.replace (/(^s*)/g, "");
}

String.prototype.RTrim = function ()
{
return This.replace (/(s*$)/g, "");
}

//-->
</script>

 


Let's take a look at what "s/s represents in the JS script.

Reference content


s matches any whitespace character, including spaces, tabs, page breaks, and so on. equivalent to [FNRTV].


Please bear in mind that the lowercase s

Related Article

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.