使用正則表達式的主要有match,exec,test
1、正則表達式方法test測試給定的字符串是否滿足正則表達式,返回值是bool類型的,只有真和假。
var user_code = $("input[name='vuser_code']").val();
var code= /[a-zA-Z0-9_]{6,16}$/;
if(!code.test(user_code)){
$.messager.alert('系統(tǒng)提示', "賬號只可以為數(shù)字、字母、'_'!", 'warning');
}
return;
2、正則表達式方法exec測試給定的字符串是否滿足正則表達式,返回匹配到的字符串,如果沒有匹配的則返回null,和test基本一致,如果需要獲取匹配的各個子字符串,可以使用下標的方式。
var text="index.aspx?test=1&ww=234";
var re = //?(/w{1,}=/w{1,}&){1,}/w{1,}=/w{1,}/;
var result= re.exec(text);
3、match其實是字符串的方法,但參數(shù)確是一個正則表達式
var text="index.aspx?test=1&ww=234";
var re = //?(/w{1,}=/w{1,}&){1,}/w{1,}=/w{1,}/;
var result= text.match(re);
function test(){
var match = new Array();
var user_code = document.getElementById("user_code").value;
var code= /[a-zA-Z0-9_]{6,16}$/;
var test = code.test(user_code); //test 測試給定的字符串是否滿足正則表達式,返回值是bool類型
var exec = code.exec(user_code); //exec 測試給定的字符串是否滿足正則表達式,返回匹配到的字符串,如果沒有匹配的則返回null
var match = user_code.match(code); // match是字符串的一個方法 ,但是其參數(shù)是一個正則表達式,并將包含查找的結(jié)果作為數(shù)組返回
alert("test-->" + test);
alert("exec-->" + exec);
for(var i=0; i<match.length;i++){
alert(match.length);
alert("match-->" + match[i]);
}
}
|
新聞熱點
疑難解答