JS removal of the string ends of the case of whitespace collation

Source: Internet
Author: User
Tags trim


Go to the left space;
function LTrim (s)
{

Return S.replace (/(^s*)/g, "");
}
Go to the right space;
function RTrim (s)
{
Return S.replace (/(s*$)/g, "");
}
Go to the left and right space;
function Trim (s) {
Return S.replace (/(^s*) | ( s*$)/g, "");
}


Remove the spaces at both ends of the string

String.prototype.trim=function () {return this.replace (^/s*) | ( /s*$)/g, ');

Remove all spaces from the string:

String.prototype.sTrim = function () {return this.replace (//s/g, ');}

Or:

var s = "ASD ddd bbb SSS";
var reg =//s/g;
var ss = S.replace (Reg, "");
Alert (ss);

The following is an enhanced version

Function: 1 Remove all space before and after the string
2 Remove all spaces in the string (including the middle space, you need to set the 2nd parameter is: g)
function Trim (Str,is_global)
{
var result;
result = Str.replace (/(^s+) | ( s+$)/g, "");
if (is_global.tolowercase () = = "G")
result = Result.replace (/s/g, "");
return result;
}

jquery Trim function

$.trim (str)

Most efficient when working with long strings.

<script type= "Text/javascript" >
Remove Space
function Systemtrim (str) {
var regExp =/(^s*) | (s*$)/;
Return Str.replace (RegExp, "");
}
Call
function Add ()
{
var Ownername=document.form1.ownername.value;
if (Systemtrim (ownername) = "")
{
Alert ("Please enter your name!") ");
Return
}
}
<script>

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.