js正则表达式验证替换用法

创建:2019年12月15日  作者:清风明月

常用字符串校验正则表达式


var REDB=new Object();

//校验通用函数
REDB.test= function (r,s){	
	return (new RegExp(r)).test(s)
}

//校验是否全由数字组成
REDB.isDigit="^[0-9]{1,20}$"

//校验登录名:只能输入5-20个以字母开头、可带数字、“_”、“.”的字串
REDB.isRegisterUserName="^[a-zA-Z]{1}([a-zA-Z0-9]|[._]){4,19}$"

//校验用户姓名:只能输入1-30个以字母开头的纯字母字串
REDB.isTrueName="^[a-zA-Z]{1,30}$"

//校验密码:只能输入6-20个字母、数字、下划线
REDB.isPasswd="^(\w){6,20}$"

//校验普通电话、传真号码:可以“+”开头,除数字外,可含有“-”
//var patrn="^[+]{0,1}(\d){1,3}[ ]?([-]?(\d){1,12})+$"
REDB.isTel="^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$"

//校验手机号码:必须以数字开头,除数字外,可含有“-”
REDB.isMobil="^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$"

//校验邮政编码
//var patrn="^[a-zA-Z0-9]{3,12}$"
REDB.isPostalCode="^[a-zA-Z0-9 ]{3,12}$"

//校验搜索关键字
REDB.isSearch="^[^`~!@#$%^&*()+=|\\\][\]\{\}:;'\,.<>/?]{1}[^`~!@$%^&()+=|\\\][\]\{\}:;'\,.<>?]{0,19}$"
REDB.isIP="^[0-9.]{1,20}$"		//by SamStudio


REDB.isNonNegativeInteger	="^\\d+$"				//非负整数(正整数 + 0)
REDB.isPositiveInteger		="^[0-9]*[1-9][0-9]*$"	//正整数
REDB.isNonPositiveInteger	="^((-\\d+)|(0+))$"		//非正整数(负整数 + 0)
REDB.isNegativeInteger		="^-[0-9]*[1-9][0-9]*$"	//负整数
REDB.isInteger				="^-?\\d+$"				//整数
REDB.isNonNegativeFloat		="^\\d+(\\.\\d+)?$"		//非负浮点数(正浮点数 + 0)
REDB.isPositiveFloat		="^(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*))$"	//正浮点数
REDB.isNonPositiveFloat		="^((-\\d+(\\.\\d+)?)|(0+(\\.0+)?))$"		//非正浮点数(负浮点数 + 0)
REDB.isNegativeFloat		="^(-(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*)))$"	//负浮点数
REDB.isFloat				="^(-?\\d+)(\\.\\d+)?$"		//浮点数
REDB.isChar					="^[A-Za-z]+$"				//由26个英文字母组成的字符串
REDB.isCharUppercase		="^[A-Z]+$"					//由26个英文字母的大写组成的字符串
REDB.isCharLowcase			="^[a-z]+$"					//由26个英文字母的小写组成的字符串
REDB.isCharNum				="^[A-Za-z0-9]+$"			//由数字和26个英文字母组成的字符串
REDB.isCharNumUnderline		="^\\w+$"					//由数字、26个英文字母或者下划线组成的字符串
REDB.isUnCharNumUnderline	="^[A-Za-z0-9_]*$"			//数字、26个英文字母和下划线以外字符组成的字符串
REDB.isEmail				="^[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+$"					//email地址
REDB.isURL					="^[a-zA-z]+://(\\w+(-\\w+)*)(\\.(\\w+(-\\w+)*))*(\\?\\S*)?$"	//url

//使用
alert(REDB.test(REDB.isDigit,'54321start'))	//false

JavaScript String 字符串对象扩展方法,正则表达式实现

/*
	lTrim():String				删除左边空格
	rTrim():String				删除右边空格
	trim():String				删除两边空格
	endsWith(String):Boolean	是否包含尾部字符
	startsWith(String):Boolean	是否包含前部字符
	format():String				C#的format写法,	("hello {0}!").format('guy') => hello guy!
	removeSpaces():String		删除所有空格
	removeExtraSpaces():String	连续的多个空格删除剩一个空格, "I  am    guy" => "I am guy"
	removeSpaceDelimitedString():String		等价: " "+removeExtraSpaces()
	isEmpty():Boolean			字符是否为空
	validateURL():Array			检验URL http://www.test.com => [:,/,/,.,.] 
	isEmail():Boolean			是否 email
	isAlphaNumeric():Boolean	是否特殊字符 /[^a-zA-Z0-9]/g
	encodeURI():String			URL编码
	decodeURI():String			URL解码
*/
String.prototype.lTrim = function () {return this.replace(/^\s*/, "");}
String.prototype.rTrim = function () {return this.replace(/\s*$/, "");}
String.prototype.trim = function () {return this.rTrim().lTrim();}
String.prototype.endsWith = function(sEnd) {return (this.substr(this.length-sEnd.length)==sEnd);}
String.prototype.startsWith = function(sStart) {return (this.substr(0,sStart.length)==sStart);}
String.prototype.format = function() { var s = this; for (var i=0; i < arguments.length; i++){ s = s.replace("{" + (i) + "}", arguments[i]);}return(s);}
String.prototype.removeSpaces = function() { return this.replace(/ /gi,'');}
String.prototype.removeExtraSpaces = function(){ return(this.replace(String.prototype.removeExtraSpaces.re, " "));}
String.prototype.removeExtraSpaces.re = new RegExp("\\s+", "g");
String.prototype.removeSpaceDelimitedString = function(r){ var s = " " + this.trim() + " "; return s.replace(" " + r,"").rTrim();}
String.prototype.isEmpty = function() {return this.length==0;}
String.prototype.validateURL = function(){ var urlRegX = /[^a-zA-Z0-9-]/g; return this.match(urlRegX, "");}
String.prototype.isEmail = function(){ var emailReg = /^\w+([-.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/; return emailReg.test(this);}
String.prototype.isAlphaNumeric = function(){ var alphaReg = /[^a-zA-Z0-9]/g; return !alphaReg.test(this);}
String.prototype.encodeURI = function(){ var returnString; returnString = escape(this);returnString = returnString.replace(/\+/g,"%2B"); return returnString}
String.prototype.decodeURI = function() {return unescape(this)}

//使用
var str="  SamStudio"
alert("["+str.lTrim()+"]")	//输出:[SamStudio]