1.email地址有效性检测 script language='JScript'> function validateEmail(emailStr) { var re=/^[\w.-]+@([0-9a-z][\w-]+\.)+[a-z]{2,3}$/i; //或 var re=new RegExp("^[\\w.-]+@([0-9a-z][\\w-]+\\.)+[a-z]{2,3}$","i"); if(re.test(emailStr)) { alert("有效email地址!"); return true; } else { alert("无效email地址!"); return false; } } /script>
2.字符串替换操作 script language='JScript'> var r, pattern, re; var s = "The rain in Spain falls mainly in the plain falls."; pattern = /falls/ig; re = s.replace(re,'falling'); alert('s = ' + s + '\n' + 're = ' + re); /script>
3.模式查找字符串 script language='JScript'> var index, pattern; var str = "four for fall fell fallen fallsing fall falls waterfalls "; pattern = /\bfalls\b/i; index = str.search(pattern); alert('The position of match is at ' + index); /script>
3.正则表达式属性例程 script language='JScript'> function matchAttrib() { var s=''; var re = new RegExp("d(b+)(d)","ig"); var str = "cdbBbdbsbdbdz"; while((arr = re.exec(str))!=null) { s += "=======================================br>"; s += "$1 returns: " + RegExp.$1 + "br>"; s += "$2 returns: " + RegExp.$2 + "br>"; s += "$3 returns: " + RegExp.$3 + "br>"; s += "input returns : " + RegExp.input + "br>"; s += "index returns : " + RegExp.index + "br>"; s += "lastIndex returns : " + RegExp.lastIndex + "br>"; s += "lastMatch returns: " + RegExp.lastMatch + "br>"; s += "leftContext returns: " + RegExp.leftContext + "br>"; s += "rightContext returns: " + RegExp.rightContext + "br>"; s += "lastParen returns: " + RegExp.lastParen + "br>"; s += "arr.index returns: " + arr.index + "br>"; s += "arr.lastIndex returns: " + arr.lastIndex + "br>"; s += "arr.input returns: " + arr.input + "br>"; s += "re.lastIndex returns: " + re.lastIndex + "br>"; s += "re.source returns: " + re.source + "br>"; } return(s); //Return results. } document.write(matchAttrib()); /script>