js正则方法test与match简单介绍
来源:互联网 发布:淘宝管家官方下载 编辑:程序博客网 时间:2024/05/17 00:08
1.写一个简单姓名校验
/***校验英文名,true:yangzi, li xiao xiao, li si, wang/wu*/function checkName(str){var reg = /^((([a-zA-Z]+ ){1,2}[a-zA-Z]+)|([a-zA-Z]+\/[a-zA-Z]+)|[a-zA-Z]+)$/;return reg.test(str);}console.log(checkName("wang wu xi"));
》true简单说下test(),exec()和match()的区别:
test(),exec()是RegExp对象方法,match()是String对象方法。
RegExp的实例有哪些属性:
- global: 布尔值,表示是否设置了 g 标志
- ignoreCase: 布尔值,表示是否设置了 i 标志
- lastIndex: 搜索下一个匹配项时开始的位置,从0开始
- multiline: 布尔值,表示是否设置了 m 标志
- source: 正则表达式的字符串表示
exec 方法受参数 g 的影响。若指定了 g,下次调用 exec 时会从上个匹配的 lastIndex 开始查找。看个例子:
0
["1", index: 2, input: "bb12"]
0
var reg = /\d/;console.log(reg.exec("bb12"));console.log(reg.lastIndex);console.log(reg.exec("bb12"));console.log(reg.lastIndex);》["1", index: 2, input: "bb12"]
0
["1", index: 2, input: "bb12"]
0
var reg = /\d/g;console.log(reg.exec("bb12"));console.log(reg.lastIndex);console.log(reg.exec("bb12"));console.log(reg.lastIndex);》["1", index: 2, input: "bb12"]
3
["2", index: 3, input: "bb12"]
4
match()可在字符串内检索指定的值,或找到一个或多个正则表达式的匹配。
var reg = /[a-c]/ig;console.log("adfBCcqwe".match(reg));》["a", "B", "C", "c"]
文章参考:https://segmentfault.com/a/1190000003497780
阅读全文
0 0
- js正则方法test与match简单介绍
- js正则函数match、search、exec、test使用介绍集合
- Js 正则表达式对象(方法:exec、test、match、search)
- 关于js的正则 test,match,exec
- 关于js的正则 test,match,exec
- Js正则基础篇 test match exec
- js正则函数match、exec、test
- js正则表达式search() 搜索、replace()替换、match()方法exec()方法test() 方法split() 方法
- 正则方法 exec 和match以及test
- js正则函数match、exec、test、search、replace、split使用介绍集合
- js正则函数match、exec、test、search、replace、split使用介绍集合
- js正则函数match、exec、test、search、replace、split使用介绍集合
- js正则函数match、exec、test、search、replace、split使用介绍
- js正则函数match、exec、test、search、replace、split使用介绍集合
- js正则函数match、exec、test、search、replace、split使用介绍集合
- js正则函数match、exec、test、search、replace、split使用介绍集合
- js正则函数match、exec、test、search、replace、split使用介绍集合
- js正则函数match、exec、test、search、replace、split使用介绍集合
- Android平台使用EasyRTMP将RTSP转RTMP流进行RTMP直播推送
- 把HTML变成图片并下载到本地
- 学习淘淘商城第一百零一课(删除购物车商品及购物车总结)
- 机会总是留给有准备的人 —— 从裁缝到码农
- 互联网时代常用网站资源整理汇总(一直在完善中)
- js正则方法test与match简单介绍
- 深度学习(二十五)基于Mutil-Scale CNN的图片语义分割、法向量估计
- springMVC中的拦截器的使用
- c++ 整数n分成k份 递归实现
- Hive-数据文件分隔符为多字符问题
- shell变量详解
- lint/checkstyle/findbugs/pmd使用汇总
- 性能测试的目标
- 关于如何在github的README.md文件内添加图片的教程