JavaScript to remove whitespace various methods summary

Source: Internet
Author: User
Tags rtrim trim

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.

Objectname.prototype
The objectname parameter is the name of the object.

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

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

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.111cn.net

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>

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

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

Please bear in mind that the lowercase s

jquery Delete Space

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


Regular substitution space

The code is as follows Copy Code
<title> IE9 js All kinds of go to space Daquan </title>
<script>
function Clearspace (Inputo) {
var Valuetext=inputo.value;
Remove space before and after
var text1=valuetext.replace (/^s+/, ""). Replace (/s+$/, "");
Remove all spaces
var text2=valuetext.replace (/[]/g, "");
Remove Front space
var text3=valuetext.replace (/^s+/, "");
Remove space after
var text4=valuetext.replace (/s+$/, "");
Alert ("= =" +text1+ "= =");
Alert ("= =" +text2+ "= =");
Alert ("= =" +text3+ "= =");
Alert ("= =" +text4+ "= =");
}
</script>
<body>
<input type= "text" id= "Appno" onblur= "clearspace (this);"/>
</body>

Traverse Delete Space

  code is as follows copy code


//To be invoked by the user
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.111cn.net
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;
}

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.