当前位置: 移动技术网 > IT编程>开发语言>JavaScript > javascript字符串函数汇总

javascript字符串函数汇总

2019年07月31日  | 移动技术网IT编程  | 我要评论

js自带函数

concat

将两个或多个字符的文本组合起来,返回一个新的字符串。

var a = "hello";
var b = ",world";
var c = a.concat(b);
alert(c);
//c = "hello,world"

indexof

返回字符串中一个子串第一处出现的索引(从左到右搜索)。如果没有匹配项,返回 -1 。

var index1 = a.indexof("l");
//index1 = 2
var index2 = a.indexof("l",3);
//index2 = 3

charat

返回指定位置的字符。

var get_char = a.charat(0);
//get_char = "h"

lastindexof

返回字符串中一个子串最后一处出现的索引(从右到左搜索),如果没有匹配项,返回 -1 。

var index1 = lastindexof('l');
//index1 = 3
var index2 = lastindexof('l',2)
//index2 = 2

match

检查一个字符串匹配一个正则表达式内容,如果么有匹配返回 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

返回字符串的一个子串,传入参数是起始位置和结束位置。

var sub_string1 = a.substring(1);
//sub_string1 = "ello"
var sub_string2 = a.substring(1,4);
//sub_string2 = "ell"

substr

返回字符串的一个子串,传入参数是起始位置和长度

var sub_string1 = a.substr(1);
//sub_string1 = "ello"
var sub_string2 = a.substr(1,4);
//sub_string2 = "ello"

replace

用来查找匹配一个正则表达式的字符串,然后使用新字符串代替匹配的字符串。

var result1 = a.replace(re,"hello");
//result1 = "hello"
var result2 = b.replace(re,"hello");
//result2 = ",world"

search

执行一个正则表达式匹配查找。如果查找成功,返回字符串中匹配的索引值。否则返回 -1 。

var index1 = a.search(re);
//index1 = 0
var index2 = b.search(re);
//index2 = -1

slice

提取字符串的一部分,并返回一个新字符串(与 substring 相同)。

var sub_string1 = a.slice(1);
//sub_string1 = "ello"
var sub_string2 = a.slice(1,4);
//sub_string2 = "ell"

split

通过将字符串划分成子串,将一个字符串做成一个字符串数组。

var arr1 = a.split("");
//arr1 = [h,e,l,l,o]

length

返回字符串的长度,所谓字符串的长度是指其包含的字符的个数。

var len = a.length();
//len = 5

tolowercase

将整个字符串转成小写字母。

var lower_string = a.tolowercase();
//lower_string = "hello"

touppercase

将整个字符串转成大写字母。

var upper_string = a.touppercase();
//upper_string = "hello"

字符串函数扩充                               

/*
===========================================
//去除左边的空格
===========================================

*/

string.prototype.ltrim = function()
{
return this.replace(/(^\s*)/g, "");
}


/*
===========================================
//去除右边的空格
===========================================
*/

string.prototype.rtrim = function()
{
return this.replace(/(\s*$)/g, "");
}

/*
===========================================
//去除前后空格
===========================================
*/

string.prototype.trim = function()
{
return this.replace(/(^\s*)|(\s*$)/g, "");
}

/*
===========================================
//得到左边的字符串
===========================================
*/

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);
}



/*
===========================================
//得到右边的字符串
===========================================
*/

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);
}



/*
===========================================
//得到中间的字符串,注意从0开始
===========================================
*/

string.prototype.mid = function(start,len)
{
return this.substr(start,len);
}


/*
===========================================
//在字符串里查找另一字符串:位置从0开始
===========================================
*/

string.prototype.instr = function(str)
{

if(str==null)
{
str = "";
}

return this.indexof(str);
}

/*
===========================================
//在字符串里反向查找另一字符串:位置0开始
===========================================
*/

string.prototype.instrrev = function(str)
{

if(str==null)
{
str = "";
}

return this.lastindexof(str);
}

/*
===========================================
//计算字符串打印长度
===========================================
*/

string.prototype.lengthw = function()
{
return this.replace(/[^\x00-\xff]/g,"**").length;
}

/*
===========================================
//是否是正确的ip地址
===========================================
*/

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;
}

}


/*
===========================================
//是否是正确的长日期
===========================================
*/

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]);

}

/*
===========================================
//是否是正确的短日期
===========================================
*/

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]);
}

/*
===========================================
//是否是正确的日期
===========================================
*/

string.prototype.isdate = function()
{
return this.islongdate()||this.isshortdate();
}

/*
===========================================
//是否是手机
===========================================
*/

string.prototype.ismobile = function()
{
return /^0{0,1}13[0-9]{9}$/.test(this);
}

/*
===========================================
//是否是邮件
===========================================
*/

string.prototype.isemail = function()
{
return /^\w+((-\w+)|(\.\w+))*\@[a-za-z0-9]+((\.|-)[a-za-z0-9]+)*\.[a-za-z0-9]+$/.test(this);
}

/*
===========================================
//是否是邮编(中国)
===========================================
*/

string.prototype.iszipcode = function()
{
return /^[\\d]{6}$/.test(this);
}

/*
===========================================
//是否是有汉字
===========================================
*/

string.prototype.existchinese = function()
{
//[\u4e00-\u9fa5]為漢字﹐[\ufe30-\uffa0]為全角符號
return /^[\x00-\xff]*$/.test(this);
}

/*
===========================================
//是否是合法的文件名/目录名
===========================================
*/

string.prototype.isfilename = function()
{
return !/[\\\/\*\?\|:"<>]/g.test(this);
}

/*
===========================================
//是否是有效链接
===========================================
*/

string.prototype.isurl = function()
{
return /^http[s]?:\/\/([\w-]+\.)+[\w-]+([\w-./?%&=]*)?$/i.test(this);
}



/*
===========================================
//是否是有效的身份证(中国)
===========================================
*/

string.prototype.isidcard = function()
{
var isum=0;
var info="";
var sid = this;

 var acity={11:"北京",12:"天津",13:"河北",14:"山西",15:"内蒙古",21:"辽宁",22:"吉林",23:"黑龙 江",31:"上海",32:"江苏",33:"浙江",34:"安徽",35:"福建",36:"江西",37:"山东",41:"河南",42:"湖 北",43:"湖南",44:"广东",45:"广西",46:"海南",50:"重庆",51:"四川",52:"贵州",53:"云南",54:"西藏",61:"陕西",62:"甘肃",63:"青海",64:"宁夏",65:"新疆",71:"台湾",81:"香港",82:"澳门",91:"国外"};

if(!/^\d{17}(\d|x)$/i.test(sid))
{
return false;
}
sid=sid.replace(/x$/i,"a");
//非法地区
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,"/"))

//非法生日
if(sbirthday!=(d.getfullyear()+"-"+ (d.getmonth()+1) + "-" + d.getdate()))
{
return false;
}
for(var i = 17;i>=0;i--)
{
isum += (math.pow(2,i) % 11) * parseint(sid.charat(17 - i),11);
}

if(isum%11!=1)
{
return false;
}
return true;

}

/*
===========================================
//是否是有效的电话号码(中国)
===========================================
*/

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);
}


/*
===========================================
//是否是数字
===========================================
*/

string.prototype.isnumeric = function(flag)
{
//验证是否是数字
if(isnan(this))
{

return false;
}

switch(flag)
{

case null:    //数字
case "":
return true;
case "+":    //正数
return        /(^\+?|^\d?)\d*\.?\d+$/.test(this);
case "-":    //负数
return        /^-\d*\.?\d+$/.test(this);
case "i":    //整数
return        /(^-?|^\+?|\d)\d+$/.test(this);
case "+i":    //正整数
return        /(^\d+$)|(^\+?\d+$)/.test(this);            
case "-i":    //负整数
return        /^[-]\d+$/.test(this);
case "f":    //浮点数
return        /(^-?|^\+?|^\d?)\d*\.\d+$/.test(this);
case "+f":    //正浮点数
return        /(^\+?|^\d?)\d*\.\d+$/.test(this);            
case "-f":    //负浮点数
return        /^[-]\d*\.\d$/.test(this);        
default:    //缺省
return true;            
}
}

/*
===========================================
//是否是颜色(#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);
}

/*
===========================================
//转换成全角
===========================================
*/

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编码
===========================================
*/

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;
}

/*
===========================================
//转换成日期
===========================================
*/

string.prototype.todate = function()
{
try
{
return new date(this.replace(/-/g, "\/"));
}
catch(e)
{
return null;
}
}

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网