js中一些常用的正则表达式收藏

/*
*remove all blanks
*trim(" df ") =df
*add by tony
*/
function trim(str) {
return str.replace(/(^\s*)|(\s*$)/g, "");
}

/*
*Round real
*Round(58.2385, 2) =58.24
*add by tony
*/
function Round(a_Num, a_Bit) {
return (Math.round(a_Num * Math.pow(10, a_Bit)) / Math.pow(10, a_Bit));
}

/*
*validate 0 or 5 bit Number(Integer)
*isValidInteger("d") = fase
*isValidInteger("2") = true
*add by tony
*/
function isValidInteger(sText) {
var reg = /^[0-9]{0,5}$/;
return reg.test(sText);
}

/*
*split text from text-value
*splitValue("ddd-fff") =ddd
*add by tony
*/
function splitText(value){
var temp = new Array();
temp = value.split("$");
return temp[0];
}

/*
*split value from text-value
*splitValue("ddd-fff") =fff
*add by tony
*/
function splitValue(value){
var temp = new Array();
temp = value.split("$");
return temp[1];
}

/* 校验是否全由数字组成 */
function isDigit(s) {
var patrn=/^[01234567891234567890]+$/;
if (!patrn.exec(s)) return false
return true
}

/*
* 全角转半角
* add by tony
*/
function DBC2SBC(str)
{
var result = '';
for (i=0 ; i<str.length; i++){
code = str.charCodeAt(i);//获取当前字符的unicode编码
if (code >= 65281 && code <= 65373){//在这个unicode编码范围中的是所有的英文字母已经各种字符
result += String.fromCharCode(str.charCodeAt(i) - 65248);//把全角字符的unicode编码转换为对应半角字符的unicode码
}else if (code == 12288) {//空格
result += String.fromCharCode(str.charCodeAt(i) - 12288 + 32);
}else {
result += str.charAt(i);
}
}
return result;
}

/************************************************
DESCRIPTION: Validates that a string contains only
valid integer number.
PARAMETERS:
strValue - String to be tested for validity
RETURNS:
True if valid, otherwise false.

Add by tonylee
**************************************************/
function validateInteger( strValue ) {
var objRegExp = /(^-?\d\d*$)/;
//check for integer characters
return objRegExp.test(strValue);
}

/*****************************************************************
DESCRIPTION: Validates that a string contains only valid numbers.
PARAMETERS:
strValue - String to be tested for validity
RETURNS:
True if valid, otherwise false.

Add by tonylee
******************************************************************/
function validateNumeric( strValue ) {
var objRegExp = /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/;
//check for numeric characters
return objRegExp.test(strValue);
}

/************************************************
DESCRIPTION: Validates that a string contains a
valid email pattern.
PARAMETERS:
strValue - String to be tested for validity
RETURNS:
True if valid, otherwise false.
REMARKS: Accounts for email with country appended
does not validate that email contains valid URL
type (.com, .gov, etc.) or valid country suffix.

Add by tonylee
*************************************************/
function validateEmail( strValue) {
var objRegExp = /(^[a-z]([a-z_\.]*)@([a-z_\.]*)([.][a-z]{3})$)|(^[a-z]([a-z_\.]*)@([a-z_\.]*)(\.[a-z]{3})(\.[a-z]{2})*$)/i;
//check for valid email
return objRegExp.test(strValue);
}

/************************************************
DESCRIPTION: Validates that a string contains valid
US phone pattern.
Ex. (999) 999-9999 or (999)999-9999
PARAMETERS:
strValue - String to be tested for validity
RETURNS:
True if valid, otherwise false.

Add by tonylee
*************************************************/
function validateUSPhone( strValue ) {
var objRegExp = /^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/;
//check for valid us phone with or without space between area code
return objRegExp.test(strValue);
}

/************************************************
DESCRIPTION: Validates that a string is not all
blank (whitespace) characters.
PARAMETERS:
strValue - String to be tested for validity
RETURNS:
True if valid, otherwise false.

Add by tonylee
*************************************************/
function validateNotEmpty( strValue ) {
var strTemp = strValue;
strTemp = trimAll(strTemp);
if(strTemp.length > 0){
return true;
}
return false;
}

/************************************************
DESCRIPTION: Trims trailing whitespace chars.
PARAMETERS:
strValue - String to be trimmed.
RETURNS:
Source string with right whitespaces removed.

Add by tonylee
*************************************************/
function rightTrim( strValue ) {
var objRegExp = /^([\w\W]*)(\b\s*)$/;
if(objRegExp.test(strValue)) {
//remove trailing a whitespace characters
strValue = strValue.replace(objRegExp, '$1');
}
return strValue;
}

/************************************************
DESCRIPTION: Trims leading whitespace chars.
PARAMETERS:
strValue - String to be trimmed
RETURNS:
Source string with left whitespaces removed.

Add by tonylee
*************************************************/
function leftTrim( strValue ) {
var objRegExp = /^(\s*)(\b[\w\W]*)$/;
if(objRegExp.test(strValue)) {
//remove leading a whitespace characters
strValue = strValue.replace(objRegExp, '$2');
}
return strValue;
}

/************************************************
DESCRIPTION: Removes leading and trailing spaces.
PARAMETERS: Source string from which spaces will
be removed;
RETURNS: Source string with whitespaces removed.

Add by tonylee
*************************************************/
function trimAll( strValue ) {
var objRegExp = /^(\s*)$/;
//check for all spaces
if(objRegExp.test(strValue)) {
strValue = strValue.replace(objRegExp, '');
if( strValue.length == 0)
return strValue;
}
//check for leading & trailing spaces
objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/;
if(objRegExp.test(strValue)) {
//remove leading and trailing whitespace characters
strValue = strValue.replace(objRegExp, '$2');
}
return strValue;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值