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>
這個腳本顯示如下結(jié)果: Found abb. Next match starts at 3 Found ab. Next match starts at 9
例子: 在下面的例子中,用戶輸入一個名字,腳本根據(jù)輸入執(zhí)行匹配操作。接著檢查數(shù)組看是否和其它用戶的名字匹配。 本腳本假定已注冊的用戶的姓已經(jīng)存進(jìn)了數(shù)組A中,或許從一個數(shù)據(jù)庫中取得。 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>