Home >Web Front-end >JS Tutorial >Extended String function method_javascript skills
I've been so busy that I don't have time to write anything of my own, but it doesn't feel good to not update my mood every day, so I can only collect some better stuff from the Internet and post it.
/*** Delete leading and trailing spaces ***/
String.prototype.Trim = function() {
return this.replace(/(^s*)|(s*$)/g, "") ;
}
/*** Count the number of occurrences of the specified character ***/
String.prototype.Occurs = function(ch) {
// var re = eval("/[^" ch " ]/g");
// return this.replace(re, "").length;
return this.split(ch).length-1;
}
/ *** Check if it consists of numbers ***/
String.prototype.isDigit = function() {
var s = this.Trim();
return (s.replace(/d/g, ""). length == 0);
}
/*** Check if it consists of numbers, letters and underscores ***/
String.prototype.isAlpha = function() {
return (this.replace(/w/g , "").length == 0);
}
/*** Check if it is a number ***/
String.prototype.isNumber = function() {
var s = this. Trim();
return (s.search(/^[ -]?[0-9.]*$/) >= 0);
}
/*** Return the number of bytes ** */
String.prototype.lenb = function() {
return this.replace(/[^x00-xff]/g,"**").length;
}
/*** Check whether it contains Chinese characters ***/
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.exec(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, successfully returns date object ***/
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 if there are characters in the list ***/
String.prototype.isInList = function(list) {
var re = eval("/[" list "]/");
return re.test(this);
}