<html> <head> <title>RegExp</title> </head> </body> <script language="javascript"> var strSrc = "xxa1b01c001yya2b02c002zz"; var re = /a(\d)b(\d{2})c(\d{3})/gi; var arr,count =0; while( (arr=re.exec(strSrc)) != null) { document.write(arr[0]+"<br>"); } </script> </body> </html>
a1b01c001
a2b02c002
<html> <head> <title>RegExp</title> </head> </body> <script language="javascript"> var strSrc = "xxa1b01c001yya2b02c002zz"; var re = /a(\d)b(\d{2})c(\d{3})/gi; var arr,count =0; arr=strSrc.match(re); for(i=0; i<arr.length; i++) { document.write(arr[i]+"<br>"); } </script> </body> </html>
a1b01c001
a2b02c002
<html> <head> <title>RegExp</title> </head> </body> <script language="javascript"> var strSrc = "a12b34c56"; var re = /(\d)(\d)/gi; var strDest = strSrc.replace(re,"$2$1"); document.write(strDest); </script> </body> </html>
a21b43c65
<html> <head> <title>RegExp</title> </head> </body> <script language="javascript"> var strSrc = "我...我要...学..学编程!"; var strDest = strSrc.replace(/\./g,"","").replace(/(.)\1/g,"$1"); document.write(strDest); </script> </body> </html>
我要学编程!
<html> <head> <title>RegExp</title> </head> </body> <script language="javascript"> var strSrc = "win a window"; var strDest = strSrc.replace(/\bwin\b/g,"lose"); document.write(strDest); </script> </body> </html>
lose a window
<html> <head> <title>RegExp</title> </head> </body> <script language="javascript"> var strSrc = "first line\n"+ "second line\n"+ "third line"; var strDest = strSrc.replace(/^/mg,"#"); document.write(strDest); </script> </body> </html>
#first line #second line #third line
<html> <head> <title>RegExp</title> </head> </body> <script language="javascript"> var reg = /(\w+):\/\/([^/:]+)(?::(\d*))?([^#]*)/; var arr1 = "http://www.it315.org:8080/index.html".match(reg); var arr2 = "http://www.it315.org/book/welcome.html#mark1".match(reg); var str = "协议,主机名,端口号,资源路径\n"; str += arr1[1] + "," + arr1[2] + "," + arr1[3] + "," + arr1[4] + "\n"; str += RegExp.$1 + "," + RegExp.$2 + "," + RegExp.$3 + "," + RegExp.$4 + "\n"; document.write(str); </script> </body> </html>
协议,主机名,端口号,资源路径 http,www.it315.org,8080,/index.html http,/book/welcome.html