DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> JavaScript基礎知識 >> javascript字符串函數匯總
javascript字符串函數匯總
編輯:JavaScript基礎知識     

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

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved