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