Extended string functions

Source: Internet
Author: User

I have been very busy and have no time to write my own things, but it is not a taste to watch the day without updating my mood. I only have to collect some of the better stuff from the Internet.
/*** Delete spaces at the beginning and end ***/
String. Prototype. Trim = function (){
Return this. Replace (/(^ \ s *) | (\ s * $)/g ,"");
}

/*** Count the number of times a specified character appears ***/
String. Prototype. occurs = function (CH ){
// Var Re = eval ("/[^" + CH + "]/g ");
// Return this. Replace (Re, ""). length;
Return this. Split (CH). Length-1;
}

/*** Check whether the data is composed of digits ***/
String. Prototype. isdigit = function (){
VaR S = This. Trim ();
Return (S. Replace (/\ D/g, ""). Length = 0 );
}

/*** Check whether it is composed of digits, letters, and underscores ***/
String. Prototype. isalpha = function (){
Return (this. Replace (/\ W/g, ""). Length = 0 );
}

/*** Check the number of items ***/
String. Prototype. isnumber = function (){
VaR S = This. Trim ();
Return (S. Search (/^ [+-]? [0-9.] * $/)> = 0 );
}

/*** Number of returned bytes ***/
String. Prototype. lenb = function (){
Return this. Replace (/[^ \ x00-\ xFF]/g, "**"). length;
}

/*** Check whether Chinese characters are contained ***/
String. Prototype. isinchinese = function (){
Return (this. length! = This. Replace (/[^ \ x00-\ xFF]/g, "**"). Length );
}

/*** Simple email check ***/
String. Prototype. isemail = function (){
VaR strr;
VaR mail = this;
VaR Re =/(\ W + @ \ W + \. \ W + )(\. {0, 1} \ W *)(\. {0, 1} \ W *)/I;
Re.exe C (Mail );
If (Regexp. $3! = "" & Regexp. $3! = "." & Regexp. $2! = ".")
Strr = Regexp. $1 + Regexp. $2 + Regexp. $3;
Else
If (Regexp. $2! = "" & Regexp. $2! = ".")
Strr = Regexp. $1 + Regexp. $2;
Else
Strr = Regexp. $1;
Return (strr = Mail );
}

/*** Simple date check. The date object is returned successfully ***/
String. Prototype. isdate = function (){
VaR P;
VaR RE1 =/(\ D {4}) [year. /-] (\ D {1, 2}) [month. /-] (\ D {1, 2}) [Day]? $ /;
VaR re2 =/(\ D {1, 2}) [month. /-] (\ D {1, 2}) [Day. /-] (\ D {2}) [year]? $ /;
VaR RE3 =/(\ D {1, 2}) [month. /-] (\ D {1, 2}) [Day. /-] (\ D {4}) [year]? $ /;
If (re1.test (this )){
P = re1.exec (this );
Return new date (P [1], p [2], p [3]);
}
If (re2.test (this )){
P = re2.exec (this );
Return new date (P [3], p [1], p [2]);
}
If (re3.test (this )){
P = re3.exec (this );
Return new date (P [3], p [1], p [2]);
}
Return false;
}

/*** Check whether there are any character characters in the list ***/
String. Prototype. isinlist = function (list ){
VaR Re = eval ("/[" + list + "]/");
Return re. Test (this );
}

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.