Implementation of the Trim method in JavaScript

Source: Internet
Author: User


The string class in Java has a trim () to remove the space characters before and after the string, and the trim () method in jquery can delete the string before and after the character variable. But there is no corresponding trim () method in JavaScript, fortunately, there are regular expressions in JavaScript, and the String object has the Replace () method, which uses JavaScript's regular and replace methods to achieve the effect of the trim () method.


The following two methods are introduced, 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, can be used to remove the space character 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


JavaScript string object has the Replace method, you can replace the characters in the string object, here we use the Replace method of the string object, the empty string before and after the replacement, to achieve the overall effect of deleting an empty string.


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


Explain the specific 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 more whitespace characters , this regular is perl-style

can also be replaced with RegExp object , can be modified to


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


Here we are just modifying the regular expression that matches the preceding space character


You can also replace 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 the space before and after the whole replacement


Replace the whole way with the previous difference is similar, the front is two times replace, with two regular to match the front and back of the space character, the whole replacement, with a regular expression, 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 , followed by the result of a reverse reference to a regular expression

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

The regular way here can also be modified to regexp way, here I do not modify.

Both of the methods described above are done 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 replace body inside the method body to achieve the effect of deleting the string before and after.




Implementation of the Trim method in JavaScript

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.