正则验证,match()与test()函数的区别?
2017-05-18 09:47
981 查看
test是RegExp的方法,参数是字符串,返回值是boolean类型。
match是String的方法,参数是正则表达式,返回值是数组。 案例:
match是String的方法,参数是正则表达式,返回值是数组。 案例:
1 //判断日期类型是否为YYYY-MM-DD格式的类型 2 function IsDate(){ 3 var str = document.getElementById('str').value.trim(); 4 if(str.length!=0){ 5 var reg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/; 6 var r = str.match(reg); 7 if(r==null) 8 alert('对不起,您输入的日期格式不正确!'); //请将“日期”改成你需要验证的属性名称! 9 } 10 }
1 //判断输入的字符是否为中文 2 function IsChinese() 3 { 4 var str = document.getElementById('str').value.trim(); 5 if(str.length!=0){ 6 reg=/^[\u0391-\uFFE5]+$/; 7 if(!reg.test(str)){ 8 alert("对不起,您输入的字符串类型格式不正确!");//请将“字符串类型”要换成你要验证的那个属性名称! 9 } 10 } 11 }
相关文章推荐
- 正则验证,match()与test()函数的区别?
- 正则表达式中 test、exec、match 方法区别
- js正则表达式中test,exec,match方法的区别说明
- Nodejs正则表达式函数之match、test、exec、search、split、replace使用详解
- python3正则表达式简单使用、search()、match()等函数的区别
- 正则表达式中 test、exec、match 方法区别
- 正则表达式中 test(),exec(),match() 方法的区别
- js正则表达式中test,exec,match方法的区别说明
- js正则表达式中test,exec,match方法的区别说明
- js正则表达式中test,exec,match方法的区别
- JavaScript中正则表达式test()、exec()、match() 方法区别
- Python中正则表达式match()、search()函数及match()和search()的区别详解
- js正则表达test、exec和match的区别
- javascript中的正则匹配函数exec(),test(),match()
- 正则表达式中 test、exec、match 方法区别
- js正则表达式中test,exec,match方法的区别说明
- 前端试题-正则中test, exec, match的区别
- python3正则表达式简单使用、search()、match()等函数的区别
- 正则表达式中 test、exec、match 方法区别
- 正则表达中常用方法小结 test(),exec(),search(),match()区别及应用。