服务器之家 > 正文
获取最后一个匹配的子表达式
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title>Untitled</title> </head> <body> <script language="JavaScript" type="text/javascript"> <!-- var strXml="<author>jack</author><author>jack</author>"; var regExp=/<author>(\w*)<\/author>/ig; //exec返回一个数组对象 var arr=strXml.match(regExp); var author=arr[1]; alert(arr); //--> alert(RegExp.$1); </script> Hello World! </body> </html>
获取全部的匹配结果:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title>Untitled</title> </head> <body> <script language="JavaScript" type="text/javascript"> <!-- var strXml = "<author>jack</author><author>jack</author>"; var regExp= /<author>(\w*)<\/author>/gi; var result = []; var match; while( match=regExp.exec(strXml) ){ result.push(match[1]); } alert(result); //--> </script> Hello World! </body> </html>