当前位置: 代码迷 >> ASP >> asp里怎么用指定分组的方式(如:$1$2)得到SubMatches
  详细解决方案

asp里怎么用指定分组的方式(如:$1$2)得到SubMatches

热度:278   发布时间:2012-03-02 14:40:28.0
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)
%>
  相关解决方案