日期:2014-05-16  浏览次数:20332 次

js正则表达式,写法---自己总结

一、/xxxx/用两个斜杠括起来

//alert(/^0\d{2,3}$/.test('0451')); //第一种(1)

?

var zhengze = /^0\d{2,3}$/;
alert(zhengze.test('0451'));//第一种(2)

?

//小数验证
function pointValidate(value){
?var txt=/^[1-9]\d{0,12}(\.[1-9]{1,2})?$/;
?if(txt.test(value)){
??return false;
?}
?return true;
}

?

?

二、new RegExp()的方式验证正则表达式

//数字验证
function numberValidate(value){
?var txt=new RegExp("^[1-9][0-9]*$");
?if(txt.test(value)){
??return false;
?}
?return true;
}

?

?