<script LANGUAGE="Javascript1.2"> myRe=/ab*/g;str = "abbcdefabh" myArray = myRe.exec(str); document.writeln("Found "+myArray[0]+". Next match starts at "+myRe.lastIndex) mySecondArray = myRe.exec(str); document.writeln("Found "+mySecondArray[0]+". Next match starts at "+myRe.lastIndex) </script>
这个脚本显示如下结果: Found abb. Next match starts at 3 Found ab. Next match starts at 9
例子: 在下面的例子中,用户输入一个名字,脚本根据输入执行匹配操作。接着检查数组看是否和其它用户的名字匹配。 本脚本假定已注册的用户的姓已经存进了数组A中,或许从一个数据库中取得。 <HTML> <script LANGUAGE="Javascript1.2"> A = ["zhao","qian","sun","li","liang"] function lookup() { firstName = //w+/i(); if (!firstName) window.alert (RegExp.input + "非法输入"); else { count=0; for (i=0;i 输入你的姓然后按回车键。 <FORM><INPUT TYPE:"TEXT" NAME="FirstName" onChange="lookup(this);"> </FORM> </HTML>