Several ways to remove spaces from JavaScript _javascript tips

Source: Internet
Author: User
Tags rtrim

method One:
Personally think the best way. Using regular expressions, this is the core principle.
Second, this method uses the prototype properties of JavaScript

In fact, you do not use this property can be implemented using the function. But this is easier to use.
Let's take a look at how this property is used.

Returns a reference to the object type prototype.

The objectname parameter is the name of the object.

Use the prototype property to provide a set of basic functions for the object's class. The operation of the new instance of the object, "inherit", gives the object a prototype. For example, to add a method that returns the maximum element value in an array for the array object. To do this, declare the function, add it to the Array.prototype, and use it.

Copy Code code as follows:

function Array_max () {
var i, max = this[0];
for (i = 1; i < this.length; i++)
if (Max < this[i])
max = This[i];
return Max;
Array.prototype.max = Array_max;
var x = new Array (1, 2, 3, 4, 5, 6);
var y = X.max ();

After the code executes, Y saves the maximum value in the array x, or says 6.

All JScript internal objects have read-only prototype properties. You can add functionality to the prototype as in this example, but the object cannot be given a different prototype. However, user-defined objects can be assigned to a new prototype.
The list of methods and attributes for each internal object in the language reference indicates which parts of the object's prototype and which are not.
The following is the original code
Copy Code code as follows:

<script language= "JavaScript" >
Source: Online Collection
Made by Yaosansi 2005-12-02
For more visit
Trim (), Ltrim (), RTrim ()

String.prototype.Trim = function ()
Return This.replace (/(^\s*) | ( \s*$)/g, "");

String.prototype.LTrim = function ()
Return This.replace (/(^\s*)/g, "");

String.prototype.RTrim = function ()
Return This.replace (/(\s*$)/g, "");


Let's take a look at what "s/s represents in the JS script.
\s matches any white space character, including spaces, tabs, page breaks, and so on. equivalent to [\f\n\r\t\v].
Please bear in mind that the lowercase s

Method Two:
Because of the simplicity of the method, there is no example here.
Copy Code code as follows:

JavaScript Go to space function
function LTrim (str) {//Remove the header space of the string
var i;
For (I=0;i if (Str.charat (i)!= "" &&str.charat (i)!= "") a break;
str = str.substring (i,str.length);
return str;
function RTrim (str) {
var i;
for (i=str.length-1;i>=0;i--) {
if (Str.charat (i)!= "" &&str.charat (i)!= "") break;
str = str.substring (0,i+1);
return str;
function Trim (str) {

Return LTrim (RTrim (str));


Method Three:
This method writes the functions together and achieves different implementations by passing the parameters differently
<HEAD> <title>javascript Trim function</title> <script language=javascript> <!--//******* Description:sinputstring is the input string, Itype is type,//0- Remove the space before and after; 1-Go leading blanks; 2-Go tail space//**************************************************************** function Ctrim (sinputstring,itype) {var Stmpstr = ' var i =-1 if (itype = 0 | | itype = = 1) {while (stmpstr = = ') {++i stmpstr = Sinputstring.substr (i,1)} sinputstring = Sinputstring.substring (i)} if (itype = 0 | | itype = = 2) {stmpstr = ' i = sinputstring.length whi Le (stmpstr = = ') {-I stmpstr = Sinputstring.substr (i,1)} sinputstring = Sinputstring.substring (0,i+1)} return sinputstring}//--> </SCRIPT> </HEAD> <BODY> javascript string removal space function (custom): <script Langu Age=javascript> <!--var sR0 = Ctrim ("T e s T", 0) var sR1 = Ctrim ("T e s T", 1) var sR2 = Ctrim ("T e s T", 2 ) document.write ("R0 = '" + sR0 + "") document.write ("R1 = '" + sR1 + "") document.write ("R2 = '" + sR2 + "")//-- > </SCRIPT> </BODY> </HTML>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

4, this method is also good
Copy Code code as follows:

LTrim () Remove the space to the left of the string
function LTrim (str)
if (Str.charat (0) = = "")
If the first character on the left of the string is a space
str = str.slice (1);//Remove the space from the string
This sentence can also be changed to str = str.substring (1, str.length);
str = LTrim (str); Recursive call
return str;
RTrim () Remove the space to the right of the string
function RTrim (str)
var ilength;
Ilength = Str.length;
if (Str.charat (iLength-1) = "")
If the first character to the right of the string is a space
str = str.slice (0, iLength-1);//Remove spaces from string
This sentence can also be changed to str = str.substring (0, iLength-1);
str = RTrim (str); Recursive call
return str;
Trim () Remove the spaces on either side of the string
function Trim (str)
Return LTrim (RTrim (str));


Copy Code code as follows:

function Trim (stext)
Return Stext.replace (New RegExp (^[\\s]*) | ( [\\s]*$] "," G ")," ");

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: 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.