日期:2014-05-17  浏览次数:20803 次

asp里如何用指定分组的方式(如:$1$2)得到SubMatches?
在ASP中Replace方法的值中可以用$1$2来替换源字符串中的对应pattern中组的信息。但如果只是获取分组$1$2应该如何做?MS的范例中SubMatches用法太麻烦,需要穷举。能否用$1$2?有代码范例吗?



------解决方案--------------------
<script>
function matchDemo(){
var s;
var re = new RegExp( "(中国)(2008)(奥运) ", "ig ");
var str = "中国2008奥运 ";
var arr = re.exec(str);
s = "$1 contains: " + RegExp.$1 + "\n ";
s += "$2 contains: " + RegExp.$2 + "\n ";
s += "$3 contains: " + RegExp.$3;
return(s);
}
alert(matchDemo());
</script>
<%
dim str:str = "中国2008奥运 "
set r = new RegExp
r.ignoreCase = true
r.global = true
r.pattern = "(中国)(2008)(奥运) "
rv = split(r.replace(str, "$1,$2,$3 "), ", ")
response.write rv(0)
%>