Summary of various methods to remove spaces in JavaScript

Source: Internet
Author: User

Below I have summarized various methods to delete spaces, including deleting spaces before and after deletion, intermediate spaces, and continuous spaces. For more information, see.

Method 1:

In my opinion, the best method is to use regular expressions, which is the core principle.
Secondly, this method uses the prototype attribute of JavaScript.

In fact, you can use functions without using this attribute, but it is more convenient to use it later.
Next let's take a look at how this attribute is used.
 
Returns a reference to an object type prototype.

ObjectName. prototype
The objectName parameter is the object name.

Description
The prototype attribute provides a set of basic functions of the object class. The new instance of the object "inherits" the operation that is granted to the object prototype.

For example, you want to add a method to the Array object to return the maximum element value in the Array. To accomplish this, declare the function, add it to Array. prototype, and use it.

The Code is as follows: Copy code

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 is executed, y saves the maximum value in array x, or 6.

All internal JScript objects have the read-only prototype attribute. You can add features for the prototype as in this example, but the object cannot be assigned different prototypes. However, user-defined objects can be assigned to new prototypes.

The method and attribute list of each internal object in this language reference specifies which parts of the object prototype and which parts are not.

Below is the original code
 
Program code

 

The Code is as follows: Copy code

<Script language = "JavaScript">

<! --

// Source: online collection

// Made by yaosansi 2005-12-02

// For more visit http://www.bKjia. c0m

// 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 ,"");

}

 

// -->

</SCRIPT>

 

Next let's take a look at "/s" in the Js script"

S matches any blank characters, including spaces, tabs, and page breaks. It is equivalent to [fnrtv].
 
Please note that it is lowercase s

 

Jquery deletes Spaces

 

The Code is as follows: Copy code
$. Trim (str)


Regular Expression Replacement space

 

The Code is as follows: Copy code
<Html>
<Head>
<Title> IE9 closed-test JS-related empty spaces </title>
</Head>
<Script>
Function clearSpace (inputO ){
Var valueText = inputO. value;
// Remove leading and trailing Spaces
Var text1 = valueText. replace (/^ s +/, ""). replace (/s + $ /,"");
// Remove all spaces
Var text2 = valueText. replace (/[]/g ,"");
// Remove the leading space
Var text3 = valueText. replace (/^ s + /,"");
// Remove the space
Var text4 = valueText. replace (/s + $ /,"");
Alert ("=" + text1 + "= ");
Alert ("=" + text2 + "= ");
Alert ("=" + text3 + "= ");
Alert ("=" + text4 + "= ");
}
</Script>
<Body>
<Input type = "text" id = "appNo" onblur = "clearSpace (this);"/>
</Body>
</Html>

Traverse and delete Spaces

The Code is as follows: Copy code


// For the user to call
Function trim (s ){
Return trimRight (trimLeft (s ));
}
// Remove the left blank
Function trimLeft (s ){
If (s = null ){
Return "";
}
Var whitespace = new String ("tnr ");
Var str = new String (s );
If (whitespace. indexOf (str. charAt (0 ))! =-1 ){
Var j = 0, I = str. length;
While (j <I & whitespace. indexOf (str. charAt (j ))! =-1 ){
J ++;
}
Str = str. substring (j, I );
}
Return str;
}
// Remove the blank www. bKjia. c0m on the right.
Function trimRight (s ){
If (s = null) return "";
Var whitespace = new String ("tnr ");
Var str = new String (s );
If (whitespace. indexOf (str. charAt (str. length-1 ))! =-1 ){
Var I = str. length-1;
While (I> = 0 & whitespace. indexOf (str. charAt (I ))! =-1 ){
I --;
}
Str = str. substring (0, I + 1 );
}
Return str;
}

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.