var str = 'aaaaaaaa' var reg1 = /a/ var reg2 = /a/g str.match(reg1) // 结果为:["a", index: 0, input: "aaaaaaaa"] str.match(reg2) // 结果为:["a", "a", "a", "a", "a", "a", "a", "a"]
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>mischen</title>
<script>
//js中使用正则表达式
function test(){
//生成正则表达式对象;
// 在g模式下,正则表达式对象的exec和test方法,依赖 正则表达式对象的lastIndex属性,而lastIndex会根据我们exec
// 和test的执行 发生偏移 如果没有相应匹配 lastIndex 重归0
//在非g模式下,正则表达式对象的exec和test方法, lastIndex 不会发生偏移
//exec方法 如果正则表达式中 有分组 第一个返回的是 匹配到的字符串 后面是根据分组分别返回的匹配的 字符串
var reg=new RegExp("\\d+[a-z]+","ig"); //字符串里 \ 表示转译
var str="123abc123def";
alert(reg.lastIndex);//0
alert(reg.exec(str));//123abc
alert(reg.lastIndex);//6
alert(reg.test(str));//true
alert(reg.lastIndex);//12
}
// test();
test1();
function test1(){
//非g模式下使用 exec 和test
var reg=new RegExp("\\d+[a-z]+","i");
var str="123abc123def";
// alert(reg.lastIndex);//0
// alert(reg.exec(str));//123abc
// alert(reg.lastIndex);//0
// alert(reg.test(str));//true
// alert(reg.lastIndex);//0
// alert(reg.exec(str));//123abc
// alert(reg.lastIndex);//0
// alert(reg.test(str));//true
// alert(reg.lastIndex);//0
var reg=new RegExp("(\\d+)([a-z]+)","i");
alert(reg.exec(str));//123abc,123,abc
alert(reg.exec(str));//123abc,123,abc
}
</script>
</head>
<body>
</body>
</html>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有