JS Self-band function
Concat
Combines the text of two or more characters to return a new string.
var a = "Hello";
var B = ", World";
var C = A.concat (b);
alert (c);
c = "Hello,world"
IndexOf
Returns the index (search from left to right) of the first occurrence of a substring in a string. If there is no match, return-1.
var index1 = A.indexof ("L");
Index1 = 2
var index2 = A.indexof ("L", 3);
INDEX2 = 3
CharAt
Returns the character at the specified position.
var Get_char = A.charat (0);
Get_char = "H"
LastIndexOf
Returns the index (search from right to left) of the last occurrence of a substring in a string, or 1 if there are no matches.
var index1 = LastIndexOf (' l ');
index1 = 3
var index2 = LastIndexOf (' l ', 2)
//index2 = 2
Match
Checks that a string matches the content of a regular expression, if no match returns NULL.
var re = new RegExp (/^\w+$/);
var is_alpha1 = A.match (re);
IS_ALPHA1 = "Hello"
var is_alpha2 = B.match (re);
IS_ALPHA2 = null
Substring
Returns a substring of a string in which the incoming argument is the starting and ending position.
var sub_string1 = a.substring (1);
Sub_string1 = "Ello"
var sub_string2 = a.substring (1,4);
Sub_string2 = "ell"
Substr
Returns a substring of a string in which the incoming argument is the starting position and length
var sub_string1 = a.substr (1);
Sub_string1 = "Ello"
var sub_string2 = a.substr (1,4);
Sub_string2 = "Ello"
Replace
Used to find a string that matches a regular expression, and then use a new string instead of a matching string.
var result1 = A.replace (Re, "Hello");
RESULT1 = "Hello"
var result2 = b.replace (Re, "Hello");
RESULT2 = ", World"
Search
Performs a regular expression-matching lookup. Returns the matching index value in the string if the lookup succeeds. otherwise return-1.
var index1 = A.search (re);
index1 = 0
var index2 = B.search (re);
Index2 =-1
Slice
Extracts part of the string and returns a new string (same as substring).
var sub_string1 = A.slice (1);
Sub_string1 = "Ello"
var sub_string2 = A.slice (1,4);
Sub_string2 = "ell"
Split
Make a string an array of strings by dividing the strings into substrings.
var arr1 = A.split ("");
ARR1 = [H,e,l,l,o]
Length
Returns the length of a string, called the length of a string, that is the number of characters it contains.
var len = A.length ();
Len = 5
toLowerCase
Converts the entire string into lowercase letters.
var lower_string = A.tolowercase ();
lower_string = "Hello"
toUpperCase
Converts the entire string to uppercase.
var upper_string = A.touppercase ();
upper_string = "HELLO"
String Function Expansion
/*
===========================================
Remove the left space
===========================================
*/
String.prototype.LTrim = function ()
{return
this.replace (/(^\s*)/g, "");
}
/*
===========================================
Remove the space on the right
===========================================
*/
String.prototype.Rtrim = function ()
{return
this.replace (/(\s*$)/g, "");
}
/*
===========================================
Remove space before and after
===========================================
*/
String.prototype.Trim = function ()
{return
this.replace (^\s*) | ( \s*$)/g, "");
/*
===========================================
Get the string on the left
===========================================
*/
String.prototype.Left = function (len)
{
if isNaN (len) | | Len==null)
{
len = this.length;
}
else
{
if (parseint (len) <0| | parseint (len) >this.length)
{
len = this.length;
}
} Return This.substr (0,len);
}
/*
===========================================
Get the string on the right
===========================================
*/
String.prototype.Right = function (len)
{
if isNaN (len) | | Len==null)
{
len = this.length;
}
else
{
if (parseint (len) <0| | parseint (len) >this.length)
{
len = this.length;
}
} Return this.substring (this.length-len,this.length);
}
/*
===========================================
Get the middle string, note starting from 0
===========================================
*/
String.prototype.Mid = function (Start,len)
{return
this.substr (Start,len);
}
/*
===========================================
Find another string in a string: position starting from 0
===========================================
*/
STRING.PROTOTYPE.INSTR = function (str)
{
if (str==null)
{
str = ' ";
}
return This.indexof (str);
/*
===========================================
To reverse find another string in a string: position 0 start
===========================================
*/
String.prototype.InStrRev = function (str)
{
if (str==null)
{
str = ' ";
}
return This.lastindexof (str);
/*
===========================================
Calculate string Print length
===========================================
*/
String.prototype.LengthW = function ()
{return
this.replace (/[^\x00-\xff]/g, "* *"). length;
}
/*
===========================================
is the correct IP address
===========================================
*/
String.prototype.isIP = function ()
{
var respacecheck =/^ (\d+) \. ( \d+) \. (\d+) \. (\d+) $/;
if (Respacecheck.test (this))
{
this.match (respacecheck);
if (regexp.$1 <= 255 && regexp.$1 >= 0
&& regexp.$2 <= 255 && regexp.$2 >= 0
&& regexp.$3 <= 255 && regexp.$3 >= 0
&& regexp.$4 <= 255 && regexp.$4 > = 0)
{return
true;
}
else
{return
false;
}
}
else
{return
false;
}
}
/*
===========================================
Whether it's the right long date
===========================================
*/
String.prototype.isLongDate = function ()
{
var r = this.replace (/(^\s*) | ( \s*$)/g, ""). Match (/^ (\d{1,4}) (-|\/) (\d{1,2}) \2 (\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2) $/);
if (r==null)
{return
false;
}
var d = new Date (r[1], r[3]-1,r[4],r[5],r[6],r[7]);
Return (D.getfullyear () ==r[1]&& (D.getmonth () +1) ==r[3]&&d.getdate () ==r[4]&&d.gethours () = =r[5]&&d.getminutes () ==r[6]&&d.getseconds () ==r[7]);
/*
===========================================
Whether it's the right short date
===========================================
*/
String.prototype.isShortDate = function ()
{
var r = this.replace (/(^\s*) | ( \s*$)/g, ""). Match (/^ (\d{1,4}) (-|\/) (\d{1,2}) \2 (\d{1,2));
if (r==null)
{return
false;
}
var d = new Date (r[1], r[3]-1, r[4]);
Return (D.getfullyear () ==r[1]&& (D.getmonth () +1) ==r[3]&&d.getdate () ==r[4]);
/*
===========================================
is the correct date
===========================================
*/
String.prototype.isDate = function ()
{return
this.islongdate () | | This.isshortdate ();
}
/*
===========================================
Whether it is a mobile phone
===========================================
*/
String.prototype.isMobile = function ()
{return
/^0{0,1}13[0-9]{9}$/.test (this);
}
/*
===========================================
Whether it is a message
===========================================
*/
String.prototype.isEmail = function ()
{return
/^\w+ (-\w+) | ( \.\w+)) *\@[a-za-z0-9]+ (\.| -) [a-za-z0-9]+) *\. [A-za-z0-9]+$/.test (this);
}
/*
===========================================
is postcode (China)
===========================================
*/
String.prototype.isZipCode = function ()
{return
/^[\\d]{6}$/.test (this);
}
/*
===========================================
Whether there is a Chinese character
===========================================
*/
String.prototype.existChinese = function ()
{
//[\u4e00-\u9fa5] for the Hans character,[\ufe30-\uffa0] return to the full horn symbol return
/^[\ X00-\xff]*$/.test (this);
}
/*
===========================================
is the legal filename/directory Name
===========================================
*/
String.prototype.isFileName = function ()
{return
!/[\\\/\*\?\|: ' <>]/g.test (this);
}
/*
===========================================
Whether it is a valid link
===========================================
*/
STRING.PROTOTYPE.ISURL = function ()
{return
/^http[s]?:\ /\/([\w-]+\.) +[\w-]+ ([\w-./?%&=]*)? $/i.test (this);
}
/*
===========================================
is the valid ID card (China)
===========================================
*/
String.prototype.isIDCard = function () {var isum=0; var info= ""; var sId = this; var acity={11: "Beijing", 12: "Tianjin", 13: "Hebei", 14: "Shanxi", 15: "Inner Mongolia", 21: "Liaoning", 22: "Jilin", 23: "Heilongjiang", 31: "Shanghai", 32: "Jiangsu", 33: "Zhejiang", 34: "Anhui", 35 : "Fujian", 36: "Jiangxi", 37: "Shandong", 41: "Henan", 42: "Hubei", 43: "Hunan", 44: "Guangdong", 45: "Guangxi", 46: "Hainan", 50: "Chongqing", 51: "Sichuan", 52: "Guizhou", 53: "Yunnan", 54: "Tibet",
61: "Shaanxi", 62: "Gansu", 63: "Qinghai", 64: "Ningxia", 65: "Xinjiang", 71: "Taiwan", 81: "Hong Kong", 82: "Macao", 91: "Foreign"};
if (!/^\d{17} (\d|x) $/i.test (sId)) {return false;} sid=sid.replace (/x$/i, "a");
Illegal area if (Acity[parseint (Sid.substr (0,2))]==null) {return false;}
var sbirthday=sid.substr (6,4) + "-" +number (Sid.substr (10,2)) + "-" +number (Sid.substr (12,2)); var d=new Date (Sbirthday.replace (/-/g, "/")//illegal Birthday if (sbirthday!= (d.getfullyear () + "-" + (D.getmonth () +1) + "-" + D.getdate ())) {return false;} for (var i = 17;i>=0;i--) {isum + = (Math.pow (2,i)%) * parseint (Sid.charat (17-i), 1
1);
} if (Isum%11!=1) {return false;} return true; }
/*
===========================================
is a valid phone number (China)
===========================================
*/
String.prototype.isPhoneCall = function ()
{return
/(^[0-9]{3,4}\-[0-9]{3,8}$) | ( ^[0-9]{3,8}$) | (^\ ([0-9]{3,4}\) [0-9]{3,8}$) | (^0{0,1}13[0-9]{9}$)/.test (this);
}
/*
===========================================
Whether it is a number
===========================================
*/
String.prototype.isNumeric = function (flag)
{
//Verify that the number
if (isNaN (this))
{return
false;
}
Switch (flag)
{case
null: //Digital Case
' ": Return
true;
Case "+": //Positive
return /(^\+?| ^\d?) \d*\.? \d+$/.test (this);
Case "-": //Negative
return /^-\d*\.? \d+$/.test (this);
Case "I": //integer
return /(^-?| ^\+?| \d) \d+$/.test (this);
Case "+i": //positive integer
return /(^\d+$) | ( ^\+?\d+$)/.test (this);
Case "I": //Negative integer
return /^[-]\d+$/.test (this);
Case "F": //Floating-point return /(^-?| ^\+?| ^\d?) \d*\.\d+$/.test (this);
Case "+f": //Positive floating-point return /(^\+?| ^\d?) \d*\.\d+$/.test (this);
Case "F": //Negative floating-point number
return /^[-]\d*\.\d$/.test (this);
Default: //defaults to return
true;
}
}
/*
===========================================
Whether it is a color (#FFFFFF形式)
===========================================
*/
String.prototype.IsColor = function ()
{
var temp = this;
if (temp== "") return true;
if (temp.length!=7) return false;
Return (Temp.search (/\#[a-fa-f0-9]{6}/)!=-1);
}
/*
===========================================
Convert Perfect angle
===========================================
*/
String.prototype.toCase = function ()
{
var tmp = ' ";
for (Var i=0;i<this.length;i++)
{
if (this.charcodeat (i) >0&&this.charcodeat (i) <255)
{
tmp + = String.fromCharCode (This.charcodeat (i) +65248);
}
else
{
tmp = = String.fromCharCode (this.charcodeat (i))
;
}
return TMP
}
/*
===========================================
HTML Encoding A string
===========================================
*/
String.prototype.toHtmlEncode = function ()
{
var str = this;
Str=str.replace (/&/g, "&");
Str=str.replace (/</g, "<");
Str=str.replace (/>/g, ">");
Str=str.replace (/\ '/g, "'");
Str=str.replace (/\ "/g," "");
Str=str.replace (/\n/g, "<br>");
Str=str.replace (/\/g, "");
Str=str.replace (/\t/g, "");
return str;
}
/*
===========================================
Convert to date
===========================================
*/
String.prototype.toDate = function ()
{
try
{return
new Date (This.replace (/-/g, "\/"));
}
catch (E)
{return
null;
}
}