Use regular expressions in javascript to delete leading and trailing spaces in strings

Source: Internet
Author: User

Instance
Copy codeThe Code is as follows:
<Script>
Var str = "yexj00 ";
Str = str. replace (/^ s * | s * $/g ,'');
Alert (str );
</Script>

Instance
Simulate RTrim:
Copy codeThe Code is as follows:
<Script>
Var str = "yexj00 ";
Str = str. replace (/s * $/g ,'');
Alert (str );
</Script>

Instance

The code and test code are as follows:
Copy codeThe Code is as follows:
<Script type = "text/javascript">
// Delete the white spaces on both sides of the string.
Function trim (str ){
Return str. replace (/^ s + | s + $/g ,'');
}
// Delete the white spaces on the left of the string.
Function ltrim (str ){
Return str. replace (/^ s +/g ,'');
}
// Delete the white space characters on the right of the string.
Function rtrim (str ){
Return str. replace (/s + $/g ,'');
}
// The following is the test code.
Var trimTest = "123456789 ";
// There is a space between the front and back.
Document. write ('length: '+ trimTest. length +' <br/> ');
// Before use
Document. write ('ltrim length: '+ ltrim (trimTest). length +' <br/> ');
// After ltrim is used
Document. write ('rtrim length: '+ rtrim (trimTest). length +' <br/> ');
// After rtrim is used
Document. write ('trim length: '+ trim (trimTest). length +' <br/> ');
// After trim is used
</Script>

The test results are as follows:
Copy codeThe Code is as follows:
Length: 11
Ltrim length: 10
Rtrim length: 10
Trim length: 9

Use a js regular expression to delete spaces before and after a string
Copy codeThe Code is as follows:
String. prototype. trim = function (){
Var reSpace =/^ \ s *(.*?) \ S * $ /;
Return this. replace (reSpace, "$1 ″);
};

Let's analyze the regular expression in the second line.
^ Start row
\ S *
Match All spaces before the character. The greedy mode is repeated.
(.*?)
Capture group, barely match any character repeatedly, that is, the character we finally need (after removing the leading and trailing spaces), this is not very easy to understand (I think)
First, I originally thought that the first character should not be written as a space ([^ \ s +]) in the capture group, however, this is completely unnecessary because \ s * in front of the capture group can capture all leading space characters, in your opinion, the character starting range of the capture group is different from that of the regular expression ~ I can't explain it clearly.
Second: Where? Its role is to barely repeat the previous characters. What does it mean? That is, if I use (. * a) to match the aaaaaaa string, the result is the (aaaaaaa) Source string. This is called greedy mode. If I use (.*? A) To match aaaaaaa, he will first match the first a, then the second a, and then the third ....... This is called stubborn pattern matching. In some cases, it is also called lazy pattern matching. A more popular one (people like the plain explanation, huh, huh) means that the former matches as many characters as possible from the back to the back, while the latter matches from the back to the back.
Third: Do we need to care about the spaces behind the capture group? Because ". "It can also match spaces. Previously, I also considered this issue to be wasted most of the time. in fact, this is the same as considering whether to exclude spaces in front of the capture group. The \ s * behind it has already been processed by us.
\ S * space after matching characters
------------------- Split line -------------------
You can customize three trim () functions to filter spaces between the left and right sides of a string.
Copy codeThe Code is as follows:
// Js remove space functions
// Add three members to the string class.
String. prototype. Trim = function () {return Trim (this );}
String. prototype. LTrim = function () {return LTrim (this );}
String. prototype. RTrim = function () {return RTrim (this );}
// Here is an independent function
Function LTrim (str)
{
Var I;
For (I = 0; I <str. length; I ++)
{
If (str. charAt (I )! = "" & Str. charAt (I )! = ") Break;
}
Str = str. substring (I, str. length );
Return str;
}
Function RTrim (str)
{
Var I;
For (I = str. length-1; I> = 0; I -)
{
If (str. charAt (I )! = "" & Str. charAt (I )! = ") Break;
}
Str = str. substring (0, I + 1 );
Return str;
}
Function Trim (str)
{
Return LTrim (RTrim (str ));
}

------------------- Split line -------------------
Copy codeThe Code is as follows:
<Script type = "text/javascript">
Function trim (str) {// Delete spaces between the left and right sides
Return str. replace (/(^ \ s *) | (\ s * $)/g, "");
}
Function ltrim (str) {// Delete the left space
Return str. replace (/(^ \ s *)/g ,"");
}
Function rtrim (str) {// Delete the right space
Return str. replace (/(\ s * $)/g ,"");
}
</Script>
Function checkSubmit (){
If (confirm ("are you sure you want to save the data ?")) {
Var AB = document. getElementById ("name"). value;
Var dj = document. getElementById ("dj"). value;
Var xy = AB. replace (/(^ \ s *) | (\ s * $)/g, "");
If (xy! = "" & Dj! = ""){
Document. dwbzjlspb. action = "";
Document. dwbzjlspb. submit ();
Return true;
} Else {
Alert ("the organization name or level to be declared cannot be blank !");
}
} Else {
Return false;
}
};

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.