在最近一个小屁项目中,没有服务端(其实服务端是人家早已经写好的服务),留给我的就只有一大堆的Html和JS,写的好烦躁,所以就写了几个扩展。
多个不说的,这里只是记录,code附上,便以后查询:
; String.format
=
function
() {
var s = arguments[ 0 ];
for ( var i = 0 ; i < arguments.length - 1 ; i ++ ) {
var reg = new RegExp( " \\{ " + i + " \\} " , " gm " );
s = s.replace(reg, arguments[i + 1 ]);
}
return s;
};
String.prototype.endsWith = function (suffix) {
return ( this .substr( this .length - suffix.length) === suffix);
};
String.prototype.startsWith = function (prefix) {
return ( this .substr( 0 , prefix.length) === prefix);
};
String.prototype.isPositiveInteger = function () {
return ( new RegExp( / ^[1-9]\d*$ / ).test( this ));
};
String.prototype.trim = function () {
return this .replace( / (^\s*)|(\s*$)|\r|\n / g, "" );
};
String.prototype.trimLeft = function () {
return this .replace( / (^\s*)|\r|\n / g, "" );
};
String.prototype.trimRight = function () {
return this .replace( / (\s*$)|\r|\n / g, "" );
};
String.prototype.isInteger = function () {
return ( new RegExp( / ^\d+$ / ).test( this ));
};
String.prototype.isNumber = function (value, element) {
return ( new RegExp( / ^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$ / ).test( this ));
};
String.prototype.isValidMail = function () {
return ( new RegExp( / ^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$ / ).test( this .trim()));
};
String.prototype.isPhone = function () {
return ( new RegExp( / (^([0-9]{3,4}[-])?\d{3,8}(-\d{1,6})?$)|(^\([0-9]{3,4}\)\d{3,8}(\(\d{1,6}\))?$)|(^\d{3,8}$) / ).test( this ));
};
String.prototype.isURL = function () {
return ( new RegExp( / ^[a-zA-z]+:\ / \ / (\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\S*)?$ / ).test( this ));
};
String.prototype.trans = function () {
return this .replace( / < / g, ' < ' ).replace( / > / g, ' > ' ).replace( / " / g, ' " ' );
};
function StringBuilder() {
var sb = new Array();
if (arguments[ 0 ] != undefined && arguments[ 0 ] != null ) {
sb[ 0 ] = arguments[ 0 ];
}
this .append = function (str) {
sb[sb.length] = str;
};
this .appendFormat = function () {
var s = arguments[ 0 ];
for ( var i = 0 ; i < arguments.length - 1 ; i ++ ) {
var reg = new RegExp( " \\{ " + i + " \\} " , " gm " );
s = s.replace(reg, arguments[i + 1 ]);
}
sb[sb.length] = s;
};
this .toString = function () {
/* var s = "";
for (var i = 0; i < sb.length; i++) {
s += sb[i];
}
return s; */
if (arguments[ 0 ] != undefined && arguments[ 0 ] != null ) {
return sb.join(arguments[ 0 ]);
}
return sb.join( "" );
};
this .replace = function (index, str) {
sb[index] = str;
};
this .replaceFormat = function () {
var s = arguments[ 1 ];
for ( var i = 0 ; i < arguments.length - 2 ; i ++ ) {
var reg = new RegExp( " \\{ " + i + " \\} " , " gm " );
s = s.replace(reg, arguments[i + 2 ]);
}
this .replace(arguments[ 0 ], s);
};
this .remove = function (index) {
for ( var i = index + 1 ; i < sb.length; i ++ ) {
sb[i - 1 ] = sb[i];
}
sb.length = sb.length - 1 ;
};
this .insert = function (index, str) {
var len = sb.length + 1 ;
for ( var i = index; i < len; i ++ ) {
sb[i + 1 ] = sb[i];
}
sb[index] = str;
};
this .insertFormat = function () {
var s = arguments[ 1 ];
for ( var i = 0 ; i < arguments.length - 2 ; i ++ ) {
var reg = new RegExp( " \\{ " + i + " \\} " , " gm " );
s = s.replace(reg, arguments[i + 2 ]);
}
this .insert(arguments[ 0 ], s);
};
this .length = function () {
return sb.length;
};
this .appendLine = function () {
if (arguments[ 0 ] != undefined && arguments[ 0 ] != null ) {
this .append(arguments[ 0 ]);
}
else {
this .append( " \r\n " );
}
}
};
/* TODO:test utils
var sb = new StringBuilder(true);
sb.append("123");
sb.appendFormat("{0}+{1}", 22, "22");
sb.appendFormat("{0}+{1}", 3333, "333");
//alert(sb.toString());
sb.insert(1, " test insert ");
sb.insertFormat(1, "{0}***{1}", 111, "11 ");
sb.replaceFormat(0, "{0}%%%{1}", 000, "00");
alert(sb.toString(" g ")); */
function Uri(urlstr) {
var uri = urlstr;
if (uri == undefined || uri == null || uri == "" ) {
uri = window.location.href;
};
this .Host = function () {
var r = uri.split( " ? " );
if (r.length > 0 ) {
return r[ 0 ];
}
return "" ;
};
this .searchString = function () {
var r = uri.split( " ? " );
if (r.length > 1 ) {
return unescape(r[ 1 ]);
}
return "" ;
};
this .Params = function () {
var search = this .searchString();
if (search == "" )
return null ;
var obj = new Array();
var pair = search.split( " & " );
if (pair.length > 0 ) {
for ( var i = 0 ; i < pair.length; i ++ ) {
var pairArr = pair[i].split( " = " );
obj[pairArr[ 0 ]] = pairArr[ 1 ];
}
}
return obj;
};
this .QueryParam = function (key, def) {
var obj = this .Params();
if (obj != null ) {
var value = obj[key];
if (value != undefined && value != null ) {
return value;
}
}
return def;
};
}
var s = arguments[ 0 ];
for ( var i = 0 ; i < arguments.length - 1 ; i ++ ) {
var reg = new RegExp( " \\{ " + i + " \\} " , " gm " );
s = s.replace(reg, arguments[i + 1 ]);
}
return s;
};
String.prototype.endsWith = function (suffix) {
return ( this .substr( this .length - suffix.length) === suffix);
};
String.prototype.startsWith = function (prefix) {
return ( this .substr( 0 , prefix.length) === prefix);
};
String.prototype.isPositiveInteger = function () {
return ( new RegExp( / ^[1-9]\d*$ / ).test( this ));
};
String.prototype.trim = function () {
return this .replace( / (^\s*)|(\s*$)|\r|\n / g, "" );
};
String.prototype.trimLeft = function () {
return this .replace( / (^\s*)|\r|\n / g, "" );
};
String.prototype.trimRight = function () {
return this .replace( / (\s*$)|\r|\n / g, "" );
};
String.prototype.isInteger = function () {
return ( new RegExp( / ^\d+$ / ).test( this ));
};
String.prototype.isNumber = function (value, element) {
return ( new RegExp( / ^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$ / ).test( this ));
};
String.prototype.isValidMail = function () {
return ( new RegExp( / ^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$ / ).test( this .trim()));
};
String.prototype.isPhone = function () {
return ( new RegExp( / (^([0-9]{3,4}[-])?\d{3,8}(-\d{1,6})?$)|(^\([0-9]{3,4}\)\d{3,8}(\(\d{1,6}\))?$)|(^\d{3,8}$) / ).test( this ));
};
String.prototype.isURL = function () {
return ( new RegExp( / ^[a-zA-z]+:\ / \ / (\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\S*)?$ / ).test( this ));
};
String.prototype.trans = function () {
return this .replace( / < / g, ' < ' ).replace( / > / g, ' > ' ).replace( / " / g, ' " ' );
};
function StringBuilder() {
var sb = new Array();
if (arguments[ 0 ] != undefined && arguments[ 0 ] != null ) {
sb[ 0 ] = arguments[ 0 ];
}
this .append = function (str) {
sb[sb.length] = str;
};
this .appendFormat = function () {
var s = arguments[ 0 ];
for ( var i = 0 ; i < arguments.length - 1 ; i ++ ) {
var reg = new RegExp( " \\{ " + i + " \\} " , " gm " );
s = s.replace(reg, arguments[i + 1 ]);
}
sb[sb.length] = s;
};
this .toString = function () {
/* var s = "";
for (var i = 0; i < sb.length; i++) {
s += sb[i];
}
return s; */
if (arguments[ 0 ] != undefined && arguments[ 0 ] != null ) {
return sb.join(arguments[ 0 ]);
}
return sb.join( "" );
};
this .replace = function (index, str) {
sb[index] = str;
};
this .replaceFormat = function () {
var s = arguments[ 1 ];
for ( var i = 0 ; i < arguments.length - 2 ; i ++ ) {
var reg = new RegExp( " \\{ " + i + " \\} " , " gm " );
s = s.replace(reg, arguments[i + 2 ]);
}
this .replace(arguments[ 0 ], s);
};
this .remove = function (index) {
for ( var i = index + 1 ; i < sb.length; i ++ ) {
sb[i - 1 ] = sb[i];
}
sb.length = sb.length - 1 ;
};
this .insert = function (index, str) {
var len = sb.length + 1 ;
for ( var i = index; i < len; i ++ ) {
sb[i + 1 ] = sb[i];
}
sb[index] = str;
};
this .insertFormat = function () {
var s = arguments[ 1 ];
for ( var i = 0 ; i < arguments.length - 2 ; i ++ ) {
var reg = new RegExp( " \\{ " + i + " \\} " , " gm " );
s = s.replace(reg, arguments[i + 2 ]);
}
this .insert(arguments[ 0 ], s);
};
this .length = function () {
return sb.length;
};
this .appendLine = function () {
if (arguments[ 0 ] != undefined && arguments[ 0 ] != null ) {
this .append(arguments[ 0 ]);
}
else {
this .append( " \r\n " );
}
}
};
/* TODO:test utils
var sb = new StringBuilder(true);
sb.append("123");
sb.appendFormat("{0}+{1}", 22, "22");
sb.appendFormat("{0}+{1}", 3333, "333");
//alert(sb.toString());
sb.insert(1, " test insert ");
sb.insertFormat(1, "{0}***{1}", 111, "11 ");
sb.replaceFormat(0, "{0}%%%{1}", 000, "00");
alert(sb.toString(" g ")); */
function Uri(urlstr) {
var uri = urlstr;
if (uri == undefined || uri == null || uri == "" ) {
uri = window.location.href;
};
this .Host = function () {
var r = uri.split( " ? " );
if (r.length > 0 ) {
return r[ 0 ];
}
return "" ;
};
this .searchString = function () {
var r = uri.split( " ? " );
if (r.length > 1 ) {
return unescape(r[ 1 ]);
}
return "" ;
};
this .Params = function () {
var search = this .searchString();
if (search == "" )
return null ;
var obj = new Array();
var pair = search.split( " & " );
if (pair.length > 0 ) {
for ( var i = 0 ; i < pair.length; i ++ ) {
var pairArr = pair[i].split( " = " );
obj[pairArr[ 0 ]] = pairArr[ 1 ];
}
}
return obj;
};
this .QueryParam = function (key, def) {
var obj = this .Params();
if (obj != null ) {
var value = obj[key];
if (value != undefined && value != null ) {
return value;
}
}
return def;
};
}
测试代码:
代码
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd%22 >
< html xmlns ="http://www.w3.org/1999/xhtml%22>
<head>
<title></title>
<style type=" text/css" >
body
{
background-color: Black;
}
div
{
border-bottom-width: 1px;
border-left-width: 0px;
border-top-width: 1px;
border-right-width: 0px;
border-style: solid;
border-color: Lime;
width: 98%;
margin: 8,8;
padding: 10px;
background-color: Black;
color: Olive;
}
</ style >
< script src ="Wolf.Utils.JSExtesion.js" type ="text/javascript" ></ script >
< script type ="text/javascript" >
function StringTest() {
document.getElementById( " result1 " ).innerHTML = String.format( " My blog : cnBlogs:{0} ,CSDN :{1} " , " http://www.cnblogs.com/whitewolf/%22, " http: // blog.csdn.net/grzx2210%22)
+ " <br/> " + " Wolf " .trim() + " Wolf " .trimLeft() + " Wolf " .trimRight() + " 122 " .isInteger();
}
function StringBuilderTest() {
var sb = new StringBuilder();
sb.appendFormat( " My blog : cnBlogs:{0} ,CSDN :{1} " , " http://www.cnblogs.com/whitewolf/%22, " http: // blog.csdn.net/grzx2210%22);
sb.append( " <br/> " );
sb.append( " remove " );
sb.remove( 2 );
sb.append( " whitewolf " );
sb.appendLine( " <br/> " );
document.getElementById( " result2 " ).innerHTML = " tostring() " + sb.toString() + " <br/>tostring(';') " + sb.toString( " ; " );
}
function UriTest() {
var uri = new Uri();
document.getElementById( " result3 " ).innerHTML = " searchString: " + uri.searchString + " <br/>id= " + uri.QueryParam( " id " , " id empty " );
}
function Test() {
StringTest();
StringBuilderTest();
UriTest();
};
</ script >
</ head >
< body onload ="Test();" >
< div id ="result1" >
</ div >
< div id ="result2" >
</ div >
< div id ="result3" >
</ div >
</ body >
</ html >
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd%22 >
< html xmlns ="http://www.w3.org/1999/xhtml%22>
<head>
<title></title>
<style type=" text/css" >
body
{
background-color: Black;
}
div
{
border-bottom-width: 1px;
border-left-width: 0px;
border-top-width: 1px;
border-right-width: 0px;
border-style: solid;
border-color: Lime;
width: 98%;
margin: 8,8;
padding: 10px;
background-color: Black;
color: Olive;
}
</ style >
< script src ="Wolf.Utils.JSExtesion.js" type ="text/javascript" ></ script >
< script type ="text/javascript" >
function StringTest() {
document.getElementById( " result1 " ).innerHTML = String.format( " My blog : cnBlogs:{0} ,CSDN :{1} " , " http://www.cnblogs.com/whitewolf/%22, " http: // blog.csdn.net/grzx2210%22)
+ " <br/> " + " Wolf " .trim() + " Wolf " .trimLeft() + " Wolf " .trimRight() + " 122 " .isInteger();
}
function StringBuilderTest() {
var sb = new StringBuilder();
sb.appendFormat( " My blog : cnBlogs:{0} ,CSDN :{1} " , " http://www.cnblogs.com/whitewolf/%22, " http: // blog.csdn.net/grzx2210%22);
sb.append( " <br/> " );
sb.append( " remove " );
sb.remove( 2 );
sb.append( " whitewolf " );
sb.appendLine( " <br/> " );
document.getElementById( " result2 " ).innerHTML = " tostring() " + sb.toString() + " <br/>tostring(';') " + sb.toString( " ; " );
}
function UriTest() {
var uri = new Uri();
document.getElementById( " result3 " ).innerHTML = " searchString: " + uri.searchString + " <br/>id= " + uri.QueryParam( " id " , " id empty " );
}
function Test() {
StringTest();
StringBuilderTest();
UriTest();
};
</ script >
</ head >
< body onload ="Test();" >
< div id ="result1" >
</ div >
< div id ="result2" >
</ div >
< div id ="result3" >
</ div >
</ body >
</ html >
效果:
作者:破 狼
出处:http://www.cnblogs.com/whitewolf/
本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。该文章也同时发布在我的独立博客中-个人独立博客、博客园--破狼和51CTO--破狼。http://www.cnblogs.com/whitewolf/archive/2010/11/19/1881490.html