JS常用常用正则表达式

javascript

浏览数:256

2019-1-8

/**JS常用常用正则表达式*/
var params = new Object();

/**匹配IPV4正则表达式*/
params.ipv4 = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])(\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])){3}$/;

/**以10开头,第二位有110或150的IPV4地址*/
params.checkip = /^(10)\.(110|150)(\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])){2}$/

/*邮箱的的正则表达式*/
params.email = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;

/**日期格式2009-07-13*/
params.date = /^\d{4}-(0?[1-9]|1[0-2])-(0?[1-9]|[1-2]\d|3[0-1])$/;

/**时间格式16:55:39*/
params.time = new RegExp("^([0-1]\\d|2[0-3]):[0-5]\\d:[0-5]\\d$");

/**由英文数字字母下划线组成的字符串*/
params.username = /^[a-zA-Z0-9_]+$/;

/**中文字符,含中文标点符号等*/
params.chinese = /^[\u0391-\uFFE5]+$/;

/**用户昵称,字母、数字、下划线、空格、+、.、中文(不含中文标点符号*/
params.nickname = /^[a-zA-Z0-9(\.)(\ )(\+)\u4E00-\uFA29\uE7C7-\uE7F3]+$/;

/**以DB开头,中间匹配任意字符,并且其中有MYSQL字符,匹配忽略大小写*/
params.db = /^(DB).*(MYSQL)/i;

/**验证字符是否为整数*/
params.int = /^[-+]?\d*$/;

/**验证双精度字符数字*/
params.double = /^[-\+]?\d+(\.\d+)?$/;

//验证函数verify('123456','int')验证是否为整数
function verify(str,pat){
    return params[pat].test(str);
}

//字符串是否为空
function  isEmpty(str) {
    var str = ''+str || '';
    if(str.length == 0){
        return true;
    }else{
        return false;
    }
}

//替换单个特殊正则字符
function privateReplace(str) {
    if(/\~/.test(str))
    str = str.replace(/\~/,'(\\~)');
    else if(/\!/.test(str))
    str = str.replace(/\!/,'(\\!)');
    else if(/\@/.test(str))
    str = str.replace(/\@/,'(\\@)');
    else if(/\#/.test(str))
    str = str.replace(/\#/,'(\\#)');
    else if(/\$/.test(str))
    str = str.replace(/\$/,'(\\$)');
    else if(/\%/.test(str))
    str = str.replace(/\%/,'(\\%)');
    else if(/\^/.test(str))
    str = str.replace(/\^/,'(\\^)');
    else if(/\&/.test(str))
    str = str.replace(/\&/,'(\\&)');
    else if(/\*/.test(str))
    str = str.replace(/\*/,'(\\*)');
    else if(/\(/.test(str))
    str = str.replace(/\(/,'(\\()');
    else if(/\)/.test(str))
    str = str.replace(/\)/,'(\\))');
    else if(/\_/.test(str))
    str = str.replace(/\_/,'(\_))');
    else if(/\+/.test(str))
    str = str.replace(/\+/,'(\\+)');
    else if(/\-/.test(str))
    str = str.replace(/\-/,'(\\-)');
    else if(/\ /.test(str))
    str = str.replace(/\ /,'(\\ )');
    else if(/\=/.test(str))
    str = str.replace(/\=/,'(\\=)');
    else if(/\[/.test(str))
    str = str.replace(/\[/,'(\\[)');
    else if(/\]/.test(str))
    str = str.replace(/\]/,'(\\])');
    else if(/\{/.test(str))
    str = str.replace(/\{/,'(\\{)');
    else if(/\}/.test(str))
    str = str.replace(/\}/,'(\\})');
    else if(/\|/.test(str))
    str = str.replace(/\|/,'(\\|)');
    else if(/\\/.test(str))
    str = str.replace(/\\/,'(\\\)');
    else if(/\;/.test(str))
    str = str.replace(/\;/,'(\\;)');
    else if(/\:/.test(str))
    str = str.replace(/\:/,'(\\:)');
    else if(/\'/.test(str))
    str = str.replace(/\'/,'(\\\')');
    else if(/\"/.test(str))
    str = str.replace(/\"/g,'(\\")');
    else if(/\,/.test(str))
    str = str.replace(/\,/,'(\\,)');
    else if(/\./.test(str))
    str = str.replace(/\./,'(\\.)');
    else if(/\//.test(str))
    str = str.replace(/\//,'(\\/)');
    else if(/\</.test(str))
    str = str.replace(/\</,'(\\<)');
    else if(/\>/.test(str))
    str = str.replace(/\>/,'(\\>)');
    else if(/\?/.test(str))
    str = str.replace(/\?/,'(\?)');
    return str;
}
//替换特殊正则字符~!@#$%^&*()-_+=[]{}|\;:'",./<>?
function replaceSpecialChar(str) {
    var str = str || '';
    var tmp = '';
    for(var i = 0;i<str.length;i++){
        tmp = tmp + privateReplace(str.substring(i,i+1));
    }
    return tmp;
}

//清楚特殊正则字符~!@#$%^&*()-_+=[]{}|\;:'",./<>?
function clearSpecialChar(str) {
    var str = str || '';
    return str.replace(/[(\ )(\~)(\!)(\@)(\#)(\$)(\%)(\^)(\&)(\*)(\()(\))(\-)(\_)(\+)(\=)(\[)(\])(\{)(\})(\|)(\\)(\;)(\:)(\')(\")(\,)(\.)(\/)(\<)(\>)(\?)(\)]+/g, '');
}

//清楚常用的特殊字符,~!@#$%^&*()-_+=[]{}|\;:'",./<>?`!@#¥……&*()&;|{}【】‘;:”“'。,、?
function clearCommonChar(str){
    var str = str || '';
    var pattern = /[(`)(‘)(!)(¥)(……)(()())(【)(】)(;)(:)(”)(“)(。)(,)(、)(?)(\~)(\!)(\@)(\#)(\$)(\%)(\^)(\&)(\*)(\()(\))(\-)(\_)(\+)(\=)(\[)(\])(\{)(\})(\|)(\\)(\;)(\:)(\')(\")(\,)(\.)(\/)(\<)(\>)(\?)(\)]+/g;
    str = str.replace(pattern,'');
        return superTrim(str);
}

//随机获取指定范围的随机整数
function randomBy(under, over){
    if(!verify(under,'int')) under = false;
    if(!verify(over,'int')) over = false;
    var under = under || 0;
    var over = over || 10;
    switch(arguments.length){
        case 1: return parseInt(Math.random()*under+1);
        case 2: return parseInt(Math.random()*(over-under+1) + under);
        default: return 0;
    }
}