1、非捕获__型分组
var str1='abc123'
var reg1=/(?:\w+?)|(?:\d+?)/g
console.log(reg1.test(str1));
console.log(str1.match(reg1));
console.log(RegExp.$1);
var reg2 = /(?:\d{4})-(\d{2})-(\d{2})/
var date2 = '2012-12-21'
reg2.test(date2)
console.log(RegExp.$1 );
console.log(RegExp.$2 );
console.log(RegExp.$3 );
console.log("----------------------");
2.1、前瞻__型分组:
var str3 = '123548king'
var reg3 = /\d*(?=king)/g
console.log(str3.match(reg3));
2.2、负前瞻__型分组:
var pi = '3.141592687**123...'
var pireg2 = /\d+(?!\.)/g
console.log(pi.match(pireg2));
2.3、后顾__型分组:
var str = 'memejiLove:Lisi,memejiLove:Zhangsan'
var reg = /(?<=memejiLove:)\w*/g
console.log(str.match(reg));
2.4、负后顾__型分组:
var str = '3.1Windows1,95Windows,NTWindows,NiubIWindows2'
var reg = /(?<!95|NT|2000)Windows\d*/g
console.log(str.match(reg));
3、应用实例(1)
var longstr=`{"departmentId":1,
"departmentName":"niubi",
"child":[{"departmentId":2,
"departmentName":"测试部门",
"child":[{"departmentId":5,
"departmentName":"试试"}
]},
{"departmentId":3,
"departmentName":"威风威风",
"child":[{"departmentId":6,"departmentName":"试试44"}]
},
{"departmentId":4,"departmentName":"测试部门2"}
]
}
`
var longreg = /(?<="departmentName"\:")(.*?)(?=")/g
console.log(longstr.match(longreg))
console.log("----------------------");