Implementation of the Trim method in JavaScript

Source: Internet
Author: User


The string class in Java has a trim () to remove space characters before and after a string. jquery also has the trim () method to remove strings before and after a character variable.

However, there is no corresponding trim () method in JavaScript. Fortunately, there are regular expressions in JavaScript, and the String object has the Replace () method. The effect of the trim () method is achieved using JavaScript's regular and replace methods.


The next two methods are described. In fact, the two methods are similar.

Is the definition of the trim () method on the prototype property of the string object and provides an implementation that, after implementation, is able to remove space characters before and after the string by using the string object name. Trim ().


String.prototype.trim = function () {    //TODO}


Method1 before and after the empty characters respectively replace


The string object for JavaScript has a replace method. The ability to replace the characters in a string object, where we take advantage of the Replace method of the string object. Replace the empty strings before and after each. The effect of deleting an empty string as a whole.


String.prototype.trim = function () {return this.replace (/^\s+/, ""). Replace (/\s+$/, ""); var sTest1 = "This is   a test sentence.    "; Alert (Stest1.trim ());


Explains the detailed method body of the trim () method

This represents the current object within the method

The first replace after this deletes the space character in front of the string

Where /^\s+/ represents the beginning of the 1 to many space characters , this regular is perl-style

can also be replaced with regexp objects . can be changed to


var regobj = new RegExp ("^\\s+"), return This.replace (Regobj, ""). Replace (/\s+$/, "");


Here we simply change the normal form of the matching preceding space character.


The same can be replaced by the following regular mode, the final code is

var regObj1 = new RegExp ("^\\s+"), var regObj2 = new RegExp ("\\s+$"), return This.replace (RegObj1, ""). Replace (RegObj2, "") ;

Method2 before and after the space total replacement


The overall substitution method is similar to the previous one. The front is two times replace, with two regular to match the front and back of the space character, the overall substitution, with a regular table, to replace the preceding space character.


String.prototype.trim = function () {return This.replace (/^\s+ (. *?)) \s+$/, "$");} var sTest2 = "This is   a new sentence.    "; Alert (Stest2.trim ());


Here return This.replace (/^\s+. *?

) \s+$/, "$");

Use a regular /^\s+ (. *?

) \s+$/ matches the space character before and after . The result of a backward reference in the following form

A value of (. *?) that represents the string to be returned before and after a space is deleted .

The regular way here can also be changed to RegExp way. I'm not going to change it here.

The two methods described above. is completed by defining a new method on the prototype property of the string object.

It is also possible to not define a new trim () method. Directly through the body of the Replace in the method body to achieve the effect of the deletion before and after the string.


Attach the final code

<script>//Method1String.prototype.trim = function () {//Return This.replace (/^\s+/, ""). Replace (/\s+$/, "");// var regobj = new RegExp ("^\\s+");//Return This.replace (Regobj, ""). Replace (/\s+$/, ""); var regObj1 = new RegExp ("^\\s+"); var regObj2 = new RegExp ("\\s+$"), return This.replace (RegObj1, ""). Replace (RegObj2, ""); var sTest1 = "This is   a test sentence.    "; Alert (Stest1.trim ());//Method2String.prototype.trim = function () {return This.replace (/^\s+ (. *?)) \s+$/, "$");} var sTest2 = "This is   a new sentence.    "; Alert (Stest2.trim ());</script>





Implementation of the Trim method in JavaScript

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.