JS trim method

Source: Internet
Author: User
Tags rtrim

In JavaScript, we need to use TRIM in many places, but JavaScript does not have an independent trim function or method to use. Therefore, we need to write a trim function to achieve our goal.
For example, Method 1:

String. Prototype. Trim = function (){
// Use a regular expression to separate spaces
// Replace it with a null string.
Return this. Replace (/(^/S *) | (/S * $)/g ,"");
}

Method 2:
Function trim (STR ){
For (VAR I = 0; I <Str. Length & Str. charat (I) = ""; I ++ );
For (var j = Str. length; j> 0 & Str. charat (J-1) = ""; j --);
If (I> J) Return "";
Return Str. substring (I, j );
}

S. In Javascript. replace (/(^/S *) | (/S * $)/g, ""); (^/S *) | (/S * $) what does that mean?

First, replace/(^/S *) | (/S * $)/g ""

Then ,/... /G indicates the place where the wildcard is placed. G indicates the global parameter. (^/S *) or (/S * $) will be replaced ""

Regular Expression matching the first and last blank characters: ^/S * |/S * $ can be used to delete the blank characters (including spaces, tabs, and page breaks) at the beginning and end of a line ), very useful expressions

 

How to Implement trim in Javascript

If you copy the file directly, the space may be faulty. Please check carefully

Function trim (STR)
{
For (VAR I = 0; I <Str. Length & Str. charat (I) = ""; I ++ );
For (var j = Str. length; j> 0 & Str. charat (J-1) = ""; j --);
If (I> J) Return "";
Return Str. substring (I, j );
}

Method 2:
// Add a function named trim
// A method of the prototype object of the string constructor.
String. Prototype. Trim = function ()
{
// Use a regular expression to separate spaces
// Replace it with a null string.
Return this. Replace (/(^/S *) | (/S * $)/g ,"");
}

// A string with spaces
VaR S = "My length ";

// Display the length before trim
Window. Alert (S + "trim length: (" + S. Length + ")");

// Delete leading and trailing Spaces
S = S. Trim ();
// Display the length after trim
Window. Alert (S + "trim length :(" + S. Length + ")");

Method 3:

// Call the VBScript function in JavaScript to construct a javascript trim Function

<HTML>
<Head>
</Head>
<Body>

<P> </P>
<Script language = VBScript>
Function vbtrimstr (temstr)
Vbtrimstr = trim (temstr)
End Function
</SCRIPT>

<Script language = JavaScript>
Function trimstr (temstr ){
Return vbtrimstr (temstr)
}
</SCRIPT>
<Form name = fmtest>
<Input type = text name = txttest>
<Input type = button name = btnok value = OK>
</Form>
<Script language = JavaScript For = btnok event = onclick>
VaR getstr=document.fmtest.txt test. Value
Alert ("*" + getstr + "*")
Getstr = trimstr (getstr)
Alert ("*" + getstr + "*")
</SCRIPT>
</Body>
</Html>

Javascript trim member functions
Use the code below to make trim a method of all strings. These are useful to place in a global Javascript file encoded by all your pages.

String. Prototype. Trim = function () {return this. Replace (/^/S + |/S + $/g ,"");}
String. Prototype. ltrim = function () {return this. Replace (/^/S + /,"");}
String. Prototype. rtrim = function () {return this. Replace (// s + $ /,"");}
// Example of using TRIM, ltrim, and rtrimvar mystring = "Hello my name is ";
Alert ("*" + mystring. Trim () + "*");
Alert ("*" + mystring. ltrim () + "*");
Alert ("*" + mystring. rtrim () + "*");

Javascript trim stand-alone functions
If you prefer not to modify the string prototype, then you can use the stand-alone functions below.

Function trim (stringtotrim) {return stringtotrim. Replace (/^/S + |/S + $/g ,"");}
Function ltrim (stringtotrim) {return stringtotrim. Replace (/^/S + /,"");}
Function rtrim (stringtotrim) {return stringtotrim. Replace (// s + $ /,"");}
// Example of using TRIM, ltrim, and rtrimvar mystring = "Hello my name is ";
Alert ("*" + trim (mystring) + "*");
Alert ("*" + ltrim (mystring) + "*");
Alert ("*" + rtrim (mystring) + "*");

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/welkin8888/archive/2008/12/18/3551413.aspx

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.