10-1 資?驗?

uqΪܪkvΡuqΦv]Regular expressions^Ob UNIX @ɤoiXӪrޥA򥻷OΤ@M榡²B\jjŸӤrAåiŦX󪺦riקΨLBCƹWAUNIX \hnΫO䴩qΪܪkAҦp grepBsedBawkBedBviBemacs C]OoǪFjuڳo˪ LKK ~|ΧaC^

Hint
YӦr½ĶARegular expressions ½uWܪkvΡuWvAOڭ̨ϥΡuqΪܪkvΡuqΦvGAaF\C

ثeUӥDys䴩 JavaScript qΪܪkASOAΩƪһPקCƹWAJavaScript qΪܪkM Perl HΨL UNIX OXG@Ҥ@ˡA]AbǨ쪺qΪܪkA]iHAΩ Perl UNIX OC]@YAuOӴΤFI^

Hint
VBScript qΦb\WM JavaScript ۦPAuLRO榡ҤPA쪺Ū̡AiHѦҺơC

JavaScript qΦO@ӤتAغcơ]Construction functoin^ RegExpA嫬ΪkpUG

re = new RegExp("pattern", "flag")
WzΪk]iH²gUC榡G
re = /pattern/flag
䤤Apattern OqΪܪkrAflag hO諸覡Cflag ȥi঳TءAOpUG

|ҨӻAڭ̪Ҧr򥻮榡AOѤ@ӭ^r[WEӼƦrզXӦApGڭ̭nDϥΪ̿JҦrANiHϥ JavaScript qΪܪkҨ榡TʡCҦpAڭ̥iΤUCӭnDϥΪ̿JҦrG

Example]regExpID01.htm^G

bWҤAڭ̥unUuҡvsAN|Is checkID() ƨӹr줤ҦriҡClXpUG

lɡ]regExpID01.htm^G]ǦϰUYi^
<html>
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=big5">
</head>

<body>
<h2 align=center>qΦG²Ҧr</h2>
<hr>

<script>
function checkID(string) {
	re = /^[A-Z]\d{9}$/;
	if (re.test(string))
		alert("\IŦXu" + re + "v榡I");
	else
		alert("ѡIŦXu" + re + "v榡I");
}
</script>
Ҧr]Ĥ@ӭ^rݤjg^G<input id=idNumber value=A12345678>
<input type=button value="" onClick="checkID(idNumber.value)">

<hr>
</body>
</html>

bWzdҤA/^[A-Z]\d{9}$/ NO@ӳqΦApUG

ѤWzAi /^[A-Z]\d{9}$/ NNiH鶴ҦrqΦC~AidNumber.value NϥΪ̿JrAre.test(string) hOqΦ re @ӤkA|Ǧ^ true falseANO_\CYnwOjg^rAuݱNWzdҪqΦ令 /^[a-zA-Z]\d{9}$/ NiHFI

Hint
`NGϥ ^ M $ANuYnŦXvCY[J ^ M $A /[A-Z]d{9}/ N|tҦrrAҦp AGF123456789 άO F1234567890 C]A[J ^ M $ iOҤ勵Tr@wOѤ@Ӥjg^r[WEӼƦrҺcC

ƹWAҦrNbsXWhAodzWhMϥΪ̪ʧOA]Yn{㪺ҡANΧ㪺ҽsXWhAŪ̥iѦҥ̫@`C

t@²檺ҤlAOnDϥΪ̿JHΥdXAoO@ 16 ӼƦrXAҦpG

Example]regExpCreditCardNumber01.htm^G

ڭ̫UuҡvsɡAJavaScript |Is checkCreditCard( ) ӹJƶiҡClXpUG

lɡ]regExpCreditCardNumber01.htm^G]ǦϰUYi^
<html>
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=big5">
</head>

<body>
<h2 align=center>qΦG²HΥdd</h2>
<hr>

<script>
function checkCreditCard(string) {
	re = /^\d{4}-\d{4}-\d{4}-\d{4}$/;
//	re = /^(\d{4}-){3}\d{4}$/;		// oؼgk]iHI
	if (re.test(string))
		alert("\IŦXu" + re + "v榡I");
	else
		alert("ѡIŦXu" + re + "v榡I");
}
</script>
HΥdXG<input id=creditCardNumber value=1234-5678-9012-3456>
<input type=button value="" onClick="checkCreditCard(creditCardNumber.value)">

<hr>
</body>
</html>

bWҤAMaA/^\d{4}-\d{4}-\d{4}-\d{4}$/ NNTHΥd榡Cܩ㪺AϥγqΦ|{X²ܦhAӥB|jj{XTʡC]ЩMe`d formValidation02.htm ݬݡC^n`NOAHΥddNbsXWhA]Yn{㪺ҡANΧ㪺HΥddsXWhAŪ̥iѦҥ̫@`C

pGƪh@ӦrAڭ̴NNݭnƪbpAAA[WѤjA]ƦơAҦpAWzdҪqΦ /^\d{4}-\d{4}-\d{4}-\d{4}$/A]iHg /^(\d{4}-){3}\d{4}$/AиոլݡI

U@ӨҤlAhOγqΪܪkҨϥΪ̪^WrAҦpG

Example]regExpEnglishName01.htm^G

ڭ̫UuҡvsɡAJavaScript |Is checkEnglishName( ) ӹJƶiҡClXpUG

lɡ]regExpEnglishName01.htm^G]ǦϰUYi^
<html>
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=big5">
</head>

<body>
<h2 align=center>qΦG²^Wr</h2>
<hr>

<script>
function checkEnglishName(string) {
	re1 = /^[A-Za-z\-]+\s+[A-Za-z\-]+$/;
	re2 = /^[A-Za-z\-]+\s+[A-Za-z\-]+\s+[A-Za-z\-]+$/;
	if (re1.test(string) || re2.test(string))
		alert("\IŦXu" + re1 + "vΡu" + re2 + "v榡I");
	else
		alert("ѡIŦXu" + re1 + "vΡu" + re2 + "v榡I");
}
</script>
A^W]榡GFirst Last  First Middle Last^G<input id=englishName value="Jyh-Shing Roger Jang">
<input type=button value="" onClick="checkEnglishName(englishName.value)">

<hr>
</body>
</html>

Wzdҵ{Aڭ̻pUG

] re1 = /^[A-Za-z\-]+\s+[A-Za-z\-]+$/ iHѨӦrJҧΦ^WrAҦp Michael JordanF re2 = /^[A-Za-z\-]+\s+[A-Za-z\-]+\s+[A-Za-z\-]+$/ hiHѤTӦrJҧΦ^WrAҦpAJyh-Shing Roger JangC

U@ӨҤlAhOγqΪܪkҹqllAҦpG

Example]regExpEmail01.htm^G

ڭ̫UuҡvsɡAJavaScript |Is checkEmail( ) ӹJƶiҡClXpUG

lɡ]regExpEmail01.htm^G]ǦϰUYi^
<html>
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=big5">
</head>

<body>
<h2 align=center>qΦG²qll榡</h2>
<hr>

<script>
function checkEmail(string) {
	re = /^.+@.+\..{2,3}$/;
	if (re.test(string))
		alert("\IŦXu" + re + "v榡I");
	else
		alert("ѡIŦXu" + re + "v榡I");
}
</script>
qllG<input id=email value="jang@cs.nthu.edu.t">
<input type=button value="" onClick="checkEmail(email.value)">

<hr>
</body>
</html>

󦹽dҩҥΨ쪺qΦ /^.+@.+\..{2,3}$/ApUG

] /^.+@.+\..{2,3}$/ iΨӤ@몺 email bAҦp test@cs.nthu.edu.tw άO roger_jang@mathworks.com AOqΦëDw|AǤX檺 email b]|令\AҦp " @math.com"AάO "test@ .tw"AάO "aa@bb.zz"CYn׶}tťժ email bAШUCdҡG

Example]regExpEmail02.htm^G

lXpUG

lɡ]regExpEmail02.htm^G]ǦϰUYi^
<html>
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=big5">
</head>

<body>
<h2 align=center>qΦGqll榡ҡ]iH׶}tťժqllb^</h2>
<hr>

<script>
function checkEmail(string) {
	re = /^[^\s]+@[^\s]+\.[^\s]{2,3}$/;
	if (re.test(string))
		alert("\IŦXu" + re + "v榡I");
	else
		alert("ѡIŦXu" + re + "v榡I");
}
</script>
qllG<input id=email value="jang@cs.n thu.edu.tw">
<input type=button value="" onClick="checkEmail(email.value)">

<hr>
</body>
</html>

󦹽dҩҥΨ쪺qΦ /^[^\s]+@[^\s]+\.[^\s]{2,3}$/ApUG

Hint
Ъ`NG^ b@qΪܪkNqOur}lmvAO@bAAhONu_wvΡuDvC

bHUdҤAڭ̳]pF@ӪAiHϥΪ̿JNrBqΦAHΤﶵAæbqΦACX쪺rAṶ̥̄iHQΦdҡAкtmAHWiqΦAѡG

Example]regExpTest01.htm^G

WzdҪlɦpUG

lɡ]regExpTest01.htm^G]ǦϰUYi^
<html>
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=big5">
</head>

<body>
<h2 align=center>qΦGխ</h2>
<hr>

<script>
function showMatched(form){
	var regexp = new RegExp(form.pattern.value, form.flag.value);
	var str = form.string.value;
	var matched = str.match(regexp);
	if (matched) {
		var dispstr = matched.length + " Ӥ쪺rG";
		for (var i=0; i<matched.length; i++)
			dispstr = dispstr + "\n" + matched[i];  
		alert(dispstr);
	} else
		alert("SrI");
}
</script>
<form>
<table align=center>
<tr><td align=right>rG
<td><input type=text size=30 name=string value="There are 10 rookies coming at 3 o'clock!">
<tr><td align=right>qΦG
<td><input type=text size=30 name=pattern value=" \w+ "> (dҡG\d{2,3}, T.*a, T.*?a, \b\w+\b)
<tr><td align=right>ﶵG
<td><input type=text size=30 name=flag value="g"> (g, i, or gi)
<tr><td align=right><br>
<td><input type="button" value="ܤ쪺r" onClick="showMatched(this.form)"><input type="reset">
</table>
</form>

<hr>
</body>
</html>

bWzdҤAڭ̨ϥΤFrꪺ match() kAӹqΦiA] matched = str.match(regexp) iN쪺re@Ӱ}CAHKBzC

Hint
bWzdҤAڭ̪qΦOu w+ v]`Ne᳣Ů^A]ҧ쪺rOu are vBu rookies vBu at v]e]Ů^Ap~ҦeᦳŮ檺rOHЦUŪ̦ۦsݬݡI

biҤeAڭiƭקAҦpnŮB^rjpgഫAoǤu@]iHѦrꪺ replace() kγqΦ exec() kӹFAoOڭ̤U@`DDC


JavaScript {]pPΡGΩΤ