(……续例19)
这里将包含19个例子的网页文件放出来,可以直接保存成网页运行,也可以下载该源文件。
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>ECMA标准(JScript & JavaScript)正则表达式 - 示例</title>
- <style>
- p
- {
- text-align:center;
- font-size:15pt;
- }
- input
- {
- width:350px;
- font-size:15pt;
- }
- span
- {
- width:50px;
- }
- </style>
- </head>
- <body>
- <script type="text/jscript">
- void function reg_01() //匹配一个数字ID
- {
- var s1 = "12-12345",
- s2 = "12-1234567",
- s3 = "1-12345",
- s4 = "12-1234",
- s5 = "1212345678";
- var r = new RegExp("^\\d{2}-\\d{5,8}$", "gim");
- for (i = 1; i <= 5; i++)
- {
- t = "alert(s" + i + ".match(r));";
- eval(t);
- }
- }
- void function reg_02() //匹配成对的HTML标记
- {
- var s1 = "<body bgcolor=\"#FFFFFF\">Mengliao</body>",
- s2 = "<h3 class=\"s1\" align=\"center\">Bosun Network</h3>",
- s3 = "<div>梦辽软件</DIV>",
- s4 = "<div>Baiyu</span>";
- var r = /^<([a-z]+[a-z0-9]*).*>.*<\/\1>$/ig;
- for (i = 1; i <= 4; i++)
- {
- t = "alert(s" + i + ".match(r));";
- eval(t);
- }
- }
- void function reg_03() //定位字符
- {
- var s = "Live for nothing,die for something";
- var r1 = /^Live for nothing,die for something$/g,
- r2 = /^Live for nothing,die for some$/g,
- r3 = /^Live for nothing,die for some/g;
- for (i = 1; i <= 3; i++)
- {
- t = "alert(s.match(r" + i + "));";
- eval(t);
- }
- }
- void function reg_04()
- {
- var s = "Live for nothing,\ndie for something"; //多行
- var r1 = /^.*/g, //匹配1次
- r2 = /^.*/mg, //匹配2次
- r3 = /^.*/m, //匹配1次
- r4 = /^.*/; //匹配1次
- for (n = 1; n <= 4; n++)
- {
- t = "alert(s.match(r" + n + ").length);";
- eval(t);
- t = "r" + n;
- for (i = 0; i < s.match(eval(t)).length; i++)
- {
- alert(s.match(eval(t))[i]);
- }
- }
- }
- void function reg_05()
- {
- var s1 = "Live for nothing,die for something",
- s2 = "Live for nothing,die for some thing";
- var r = /\bthing\b/g;
- alert(s1.match(r)); //匹配0次
- r = /thing\b/g;
- alert(s1.match(r)); //匹配2次
- r = /\bthing\b/g;
- alert(s2.match(r)); //匹配1次
- r = /\bfor something\b/g;
- alert(s1.match(r)); //匹配1次
- alert(s2.match(r)); //匹配0次
- }
- void function reg_06()
- {
- var s1 = "1024",
- s2 = "+1024",
- s3 = "1,024",
- s4 = "1",
- s5 = "-1024",
- s6 = "10000";
- var r = /^\+?[1-9],?\d{3}$/g;
- for (i = 1; i <= 6; i++)
- {
- t = "alert(s" + i + ".match(r));"
- eval(t);
- }
- }
- void function reg_07()
- {
- var s1 = "0",
- s2 = "0.23",
- s3 = "100",
- s4 = "100.01",
- s5 = "9.9",
- s6 = "99.9",
- s7 = "99.",
- s8 = "00.1",
- s9 = "05",
- s10 = "100.00",
- s11 = "100.",
- s12 = "+100.000";
- var r = /^\+?((100(.0+)?)|([1-9]?[0-9])(\.\d+)?)$/g; //匹配0到100的数
- for (i = 1; i <= 12; i++)
- {
- t = "alert(s" + i + ".match(r));"
- eval(t);
- }
- }
- void function reg_08()
- {
- var s = "Mengliao Software Studio";
- var r1 = /\t/g, //匹配1次
- r2 = /\s/g; //匹配2次
- for (i = 1; i <= 2; i++)
- {
- t = "alert(s.match(r" + i + "));"
- eval(t);
- }
- }
- void function reg_09()
- {
- var s1 = "Live for nothing,die for something",
- s2 = "Live for nothing,die for somebody",
- s3 = "Live for noabcde,die for someabcde";
- var r = /^Live ([a-z]{3}) no([a-z]{5}),die \1 some\2$/g;
- for (i = 1; i <= 3; i++)
- {
- t = "alert(r.exec(s" + i + "));"
- eval(t);
- }
- }
- void function reg_10()
- {
- var s = "Live for nothing,die for something";
- var r = /^Live ([a-z]{3}) no([a-z]{5}),die \1 some\2$/g;
- if (r.exec(s) != null)
- {
- alert("全局捕获组1: " + RegExp.$1 +
- "\n全局捕获组2: " + RegExp.$2); //只能使用全局RegExp
- }
- }
- void function reg_11()
- {
- var s = "Live for nothing";
- var r = /^Live for no(?:[a-z]{5})$/g;
- if (r.exec(s) != null)
- {
- alert("全局捕获组1: " + RegExp.$1);
- }
- }
- void function reg_12()
- {
- var ver = Number(ScriptEngineMajorVersion() + "." + ScriptEngineMinorVersion())
- if (ver >= 5.5)
- {
- var s = "Live for nothing,die for something";
- var r = /\w+/g;
- while ((r.exec(s)) != null)
- alert(RegExp.index + "-" + RegExp.lastIndex + ": " + RegExp.lastMatch);
- }
- else
- {
- alert("只有JScript 5.5及以上版本支持该功能。");
- }
- }
- void function reg_13()
- {
- var ver = Number(ScriptEngineMajorVersion() + "." + ScriptEngineMinorVersion())
- if (ver >= 5.5)
- {
- var s = "Live for nothing,die for something";
- var r = /\w+/g;
- var arr;
- while ((arr = r.exec(s)) != null)
- alert(arr.index + "-" + arr.lastIndex + ": " + arr);
- }
- else
- {
- alert("只有JScript 5.5及以上版本支持该功能。");
- }
- }
- void function reg_14()
- {
- var s = "Live for nothing nothing";
- var r = /([a-z]+) \1/g;
- if (r.exec(s) != null)
- {
- alert(RegExp.$1);
- alert(s.replace(r, RegExp.$1)); //JScript不支持命名组
- }
- }
- void function reg_15()
- {
- var s = "Live for nothing,die for something";
- var r = /.*thing/; //默认贪婪模式
- alert(s.match(r));
- r = /.*?thing/; //非贪婪模式
- alert(s.match(r));
- }
- void function reg_16()
- {
- var s = "Live for nothing,die for something";
- var r = /.*thing,/g; //默认回溯模式
- alert(s.match(r));
- //r = /(?>.*)thing,/g; //JScript不支持非回溯模式
- //alert(s.match(r));
- }
- void function reg_17()
- {
- var s = "1024 used 2048 free";
- var r = /\d{4}(?= used)/g; //4位数字后紧跟" used"
- alert(s.match(r));
- r = /\d{4}(?! used)/g; //4位数字后不能跟有" used"
- alert(s.match(r));
- //JScript不支持逆序正向和逆序负向预搜索
- }
- void function reg_18()
- {
- var s1 = "Mengliao Software",
- s2 = "梦辽软件 - Bosun Network Co., Ltd.",
- s3 = "一龻", //\u4e00, \u9fbb
- s4 = "�瑕棣軎�"; //CJK兼容字符
- var r = /[\u4e00-\u9fbb]/g;
- for (i = 1; i <= 4; i++)
- {
- t = "alert(s" + i + ".match(r));"
- eval(t);
- }
- }
- void function reg_19(s)
- {
- var r = /^\+?0*(?:100(\.0*)?|(\d{0,2}(?=\.\d)|\d{1,2}(?=($|\.$)))(\.\d*)?)$/g;
- if (s.match(r) != null)
- {
- alert("匹配正常!");
- }
- else
- {
- alert("匹配失败!");
- }
- }
- </script>
- <p>ECMA标准(JScript & JavaScript)正则表达式 - 示例</p>
- <p>转换自《梦辽.NET(C#)高级编程讲义》C08.S02</p>
- <p>Ver 1.0.0 Build 20090607</p>
- <p>
- <input type="button" value="01.匹配一个数字ID" onClick="reg_01()" />
- <span></span>
- <input type="button" value="02.匹配成对的HTML标记" onClick="reg_02()" />
- <br /><br />
- <input type="button" value="03.定位符匹配" onClick="reg_03()" />
- <span></span>
- <input type="button" value="04.多行、全文定位符匹配" onClick="reg_04()" />
- <br /><br />
- <input type="button" value="05.单词定位符匹配" onClick="reg_05()" />
- <span></span>
- <input type="button" value="06.重复描述符匹配" onClick="reg_06()" />
- <br /><br />
- <input type="button" value="07.择一匹配" onClick="reg_07()" />
- <span></span>
- <input type="button" value="08.非打印字符匹配" onClick="reg_08()" />
- <br /><br />
- <input type="button" value="09.捕获组匹配" onClick="reg_09()" />
- <span></span>
- <input type="button" value="10.全局获取捕获组匹配的内容" onClick="reg_10()" />
- <br /><br />
- <input type="button" value="11.非捕获组匹配" onClick="reg_11()" />
- <span></span>
- <input type="button" value="12.全局RegExp属性" onClick="reg_12()" />
- <br /><br />
- <input type="button" value="13.全局RegExp属性的另一种实现" onClick="reg_13()" />
- <span></span>
- <input type="button" value="14.全局组替换" onClick="reg_14()" />
- <br /><br />
- <input type="button" value="15.非贪婪匹配" onClick="reg_15()" />
- <span></span>
- <input type="button" value="16.贪婪模式下的回溯匹配" onClick="reg_16()" />
- <br /><br />
- <input type="button" value="17.顺序正向、顺序负向预搜索匹配" onClick="reg_17()" />
- <span></span>
- <input type="button" value="18.十六进制转义字符(汉字)匹配" onClick="reg_18()" />
- <br /><br />
- <input type="text" id="num" />
- <span></span>
- <input type="button" value="19.[0, 100]比较完备的匹配" onClick="reg_19(num.value)" />
- </p>
- <p>山西大同大学 数计学院 白宇</p>
- <p>版权所有:梦辽软件 2009</p>
- <p>Copyrights: Mengliao Software, 2009</p>
- <!--
- //匹配协议名、主机名、域名、目录名、文件名
- string[] name = { "URI", "Protocol", "Host", "Domain", "Port", "Folder", "File" };
- string[] URI = { @"http://www.bosun-net.com:8000/mengliao/cs/index.html", @"ftp://g.cn:21/list.zip", @"http://memory.zol.com.cn/more/" };
- string URIPattern = @"^([a-z]{3,})://(?:([a-z0-9_-]*)\.)?((?:[a-z0-9_-]+\.?){3,})(?::([0-9]+))?((?:/[a-z0-9._-]+)*/)([a-z0-9._-]*)/?$";
- for (int i = 0; i < URI.Length; i++)
- {
- Match match = Regex.Match(URI[i], URIPattern, RegexOptions.IgnoreCase);
- int j = 0;
- foreach (Group x in match.Groups)
- Console.WriteLine("{0}: {1}", name[j++], x.Value);
- Console.WriteLine();
- }
- -->
- </body>
- </html>
运行截图如下:
可以从后面的附件或这里的链接下载该源文件:http://img1.51cto.com/attachment/201012/876134_1293810016.rar
(全文完)