Using regular expressions in JavaScript to implement the delete before and after spaces _ regular expressions in a string

Source: Internet
Author: User
Tags rtrim
Instance
Copy Code code as follows:

<script>
var str= "yexj00";
Str=str.replace (/^s*|s*$/g, "");
alert (str);
</script>

Instance
Imitation RTrim:
Copy Code code as follows:

<script>
var str= "yexj00";
Str=str.replace (/s*$/g, "");
alert (str);
</script>

Instance

The code and test code are as follows:
Copy Code code as follows:

<script type= "Text/javascript" >
Deletes white space characters on either side of the string.
function Trim (str) {
Return Str.replace (/^s+|s+$/g, "");
}
Deletes the white space character to the left of the string.
function LTrim (str) {
Return Str.replace (/^s+/g, "");
}
Deletes the white space character to 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 before and after.
document.write (' Length: ' +trimtest.length+ ' <br/> ');
Before use
document.write (' LTrim length: ' +ltrim (trimtest). length+ ' <br/> ');
After using LTrim
document.write (' RTrim length: ' +rtrim (trimtest). length+ ' <br/> ');
After using RTrim
document.write (' Trim length: ' +trim (trimtest). length+ ' <br/> ');
After using trim
</script>

The results of the test are as follows:
Copy Code code as follows:

Length:11
LTrim length:10
RTrim Length:10
Trim Length:9

Use JS Regular expression to delete the string before and after the space
Copy Code code as follows:

String.prototype.trim=function () {
var respace=/^\s* (. *?) \s*$/;
Return This.replace (Respace, "$1″");
};

Let's analyze the regular expression in the second row
^ Line Start
\s*
Matches all spaces before the character, greedy mode repeats
(.*?)
Capturing group, the reluctance pattern repeats any character, that is, we ultimately need (remove the front and back space after the characters, this is not very well understood (I think)
First: I originally thought that the first character should not be written as a space ([^\s+]) in the capturing group, but it was completely unnecessary because the \s* in front of the capturing group had been able to capture all the whitespace characters at the beginning. You think the capturing group's character start range is different from the regular expression matching character range, Khan ~ a bit unclear, hehe
Second: The role of which, his role is to reluctantly repeat the preceding characters, what does that mean? If I use (. *a) to match the AAAAAAA string, the result is (AAAAAAA) source string This is called greedy mode, if you use (. *?a) To match AAAAAAA, he's going to match first A, then the second A, then the third a ... it's called a grudging pattern match. Some places are also called lazy pattern matching. Popular point (everyone likes the popular explanation, hehe) is that the former is from the back to go to match as many characters as possible, and the latter is the former match
Third: Do we have to care about the space behind the capturing group? Because the "." In the capturing group Also can match the space, previously I also consider this problem most of the time is wasted on this. In fact, this and consider whether to exclude the capture group before the space the same reason, the back of the \s* has been for us to deal with
\s* a space after the matching character
——————————————————— Split Line —————————————————— –
Customize the three trim () function to filter the space between the left and right sides of the string.
Copy Code code as follows:

JS Remove Space function
Add three members here for 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 a standalone 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 Code code as follows:

<script type= "text/javascript" >
function Trim (str) {//Remove spaces at left and right ends
Return Str.replace (/(^\s*) | ( \s*$)/g, "");
}
Function LTrim (str) {//delete left space
return str.replace (/(^\s*)/g, "");
}
Function RTrim (str) {//delete the right space
return Str.replace (/(\s*$)/g, "");
}
</script>
function Checksubmit () {
If confirm ("Confirm save 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 unit name or the intended reporting level cannot be empty!") ”);
}
}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.