JS中常用到的一些操作字符串的函数
//去除字符串两端空格
String.prototype.Trim = function(){
try{
return this.replace(/(^\s*)|(\s*$)/g, "");
}catch(e){
}
}
//求字符串字节长度
String.prototype.LenB = function(){
try{
return this.replace(/[^\x00-\xff]/g,"**").length;
} catch(e){
}
}
//得到字符串字符长度
function strlen(str) {
var len;
var i;
len = 0;
for (i=0;i<str.length;i++) {
if (str.charCodeAt(i)>255) len+=2;
else len++;
}
return len;
}
//判断是否是数字
function IsNumeric(n){
try{
return /^\d+$/.test(n);
}catch(e){
}
}
//替换特殊字符串
function replaceSpecial(str)
{
try{
return str.replace(/\&/g,"&").replace(/</g,"<").replace(/>/g,">");
}
catch(e){
//DispErrorWin("系统错误","系统错误,请升级成为IE6.0或留言,谢谢!");
}
}
// 清除WORD冗余格式并粘贴
function CleanWord( html ) {
try{
html = html.replace(/<\/?SPAN[^>]*>/gi, "" );
html = html.replace(/<(\w[^>]*) class=([^ |>]*)([^>]*)/gi, "<$1$3") ;
html = html.replace(/<(\w[^>]*) style="([^"]*)"([^>]*)/gi, "<$1$3") ;
html = html.replace(/<(\w[^>]*) lang=([^ |>]*)([^>]*)/gi, "<$1$3") ;
html = html.replace(/<\\?\?xml[^>]*>/gi, "") ;
html = html.replace(/<\/?\w+:[^>]*>/gi, "") ;
html = html.replace( /(<P)([^>]*>.*?)(<\/P>)/gi, "$2<br>" );
html = html.replace(/ /, " " );
html = ClearLimiteWord(html);
return html;
}
catch(e)
{
//DispErrorWin("系统错误","系统错误,请升级成为IE6.0或留言,谢谢!");
}
}
//过滤script脚本
function ClearLimiteWord(s){
var s1;
s1=s;
try{
s1=s1.replace(/<script((.|\n)*?)<\/script>/gi,"");
}catch(e){}
s1=s1.replace(/onload|onclick|onkeypress|onkeydown|onselectstart|ondragstart|onmouseover|onmouseout|onmousedown|onmouseup|oncopy|ondblclick|onpaste|onfocus|onunload|onbeforeunload|oncontextmenu|onblur|name|id/gi,"o");
return s1;
}
//是否是日期
String.prototype.isDate = function()
{
try {
var r = this.match(/^(\d{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]);
} catch(e) {
//DispErrorWin("系统错误","系统错误,请升级成为IE6.0或留言,谢谢!");
}
}
//判断是否包启中文
///[\u4E00-\u9FA5]/g
String.prototype.isContainsChinese=function(){
try{
return /[\u4E00-\u9FA5]/g.test(this);
}catch(e){
}
}