Extended String function method_javascript skills

Source: Internet
Author: User
The String extension method has been very busy and has no time to write your own things. But it is not a taste to watch the day without updating your mood. You only have to collect some good 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 );
}
Related Article

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.