¡u³q¥Îªí¥Üªk¡v©Î¡u³q¥Î¦¡¡v¡]Regular expressions¡^¬O¦b UNIX ¥@¬É¤¤µo®i¥X¨Óªº¦r¦ê¤ñ¹ï§Þ¥©¡A¨ä°ò¥»·§©À¬O¥Î¤@®M®æ¦¡Â²³æ¡B¦ý¥\¯à±j¤jªº²Å¸¹¨Ó¤ñ¹ï½ÆÂøªº¦r¦ê¡A¨Ã¥i¹ï²Å¦X¤ñ¹ï±ø¥óªº¦r¦ê¶i¦æקï©Î¨ä¥L¹Bºâ¡C¨Æ¹ê¤W¡AUNIX ªº³\¦h³nÅé©Î«ü¥O³£¤ä´©³q¥Îªí¥Üªk¡A¨Ò¦p grep¡Bsed¡Bawk¡Bed¡Bvi¡Bemacs µ¥¡C¡]¦ý¬O³o¨ÇªF¦è¤j·§¥u¦³¹³§Ú³o¼Ëªº LKK ¤~·|¥Î§a¡C¡^¥Ø«e¦UÓ¥D¬yÂsÄý¾¹³£¤ä´© JavaScript ªº³q¥Îªí¥Üªk¡A¯S§O¾A¥Î©óªí³æ¸ê®ÆªºÅçÃÒ»Pקï¡C¨Æ¹ê¤W¡AJavaScript ªº³q¥Îªí¥Üªk©M Perl ¥H¤Î¨ä¥L UNIX ¬ÛÃö«ü¥O´X¥G¤@¼Ò¤@¼Ë¡A¦]¦¹¡A¦b¥»³¹¾Ç¨ìªº³q¥Îªí¥Üªk¡A¤]¥i¥H§¹¥þ¾A¥Î©ó Perl ©Î UNIX ¬ÛÃö«ü¥O¡C¡]¤@³½¨â¦Y¡A¯u¬O¤Ó´Î¤F¡I¡^
JavaScript ªº³q¥Î¦¡¬O¤@Ó¤º«Øªºª«¥ó¡A¨ä«Øºc¨ç¼Æ¡]Construction functoin¡^¬° RegExp¡A¨å«¬¥Îªk¦p¤U¡G
¤Wz¥Îªk¤]¥i¥H²¼g¦¨¤U¦C®æ¦¡¡Gre = new RegExp("pattern", "flag") ¨ä¤¤¡Apattern ¬O³q¥Îªí¥Üªkªº¦r¦ê¡Aflag «h¬O¤ñ¹ïªº¤è¦¡¡Cflag ªºÈ¥i¯à¦³¤TºØ¡A¤À§O¸ÑÄÀ¦p¤U¡Gre = /pattern/flag
- g¡G¥þ°ì¤ñ¹ï¡]Global match¡^
- i¡G©¿²¤¤j¤p¼g¡]Ignore case¡^
- gi¡G¥þ°ì¤ñ¹ï¨Ã©¿²¤¤j¤p¼g
Á|¨Ò¨Ó»¡¡A§Ú̪º¨¥÷ÃÒ¦r¸¹ªº°ò¥»®æ¦¡¡A¬O¥Ñ¤@Ó^¤å¦r¥À¥[¤W¤EӼƦr²Õ¦X¦Ó¦¨¡A¦pªG§ÚÌn¨D¨Ï¥ÎªÌ¿é¤J¨¥÷ÃÒ¦r¸¹¡A´N¥i¥H¨Ï¥Î JavaScript ªº³q¥Îªí¥Üªk¨ÓÅçÃÒ¨ä®æ¦¡ªº¥¿½T©Ê¡C¨Ò¦p¡A§ÚÌ¥i¥Î¤U¦Cªí³æ¨Ón¨D¨Ï¥ÎªÌ¿é¤J¨¥÷ÃÒ¦r¸¹¡G
¦b¤W¨Ò¤¤¡A§ÚÌ¥un«ö¤U¡uÅçÃÒ¡vªº«ö¶s¡A´N·|©I¥s checkID() ¨ç¼Æ¨Ó¹ï¤å¦rÄæ¦ì¤¤ªº¨¥÷ÃÒ¦r¸¹¶i¦æÅçÃÒ¡C¬ÛÃöì©l½X¦p¤U¡G
¦b¤Wz½d¨Ò¤¤¡A/^[A-Z]\d{9}$/ ´N¬O¤@Ó³q¥Î¦¡¡A»¡©ú¦p¤U¡G
¥Ñ¤Wz»¡©ú¡A¥iª¾ /^[A-Z]\d{9}$/ ´N¥Nªí¥i¥H¤ñ¹ï¨¥÷ÃÒ¦r¸¹ªº³q¥Î¦¡¡C¦¹¥~¡AidNumber.value ¥Nªí¨Ï¥ÎªÌ¿é¤Jªº¦r¦ê¡Are.test(string) «h¬O³q¥Î¦¡ re ªº¤@Ó¤èªk¡A·|¶Ç¦^ true ©Î false¡A¥Nªí¤ñ¹ï¬O§_¦¨¥\¡CYn¤£©w¬O¤j¼g^¤å¦r¥À¡A¥u»Ý±N¤Wz½d¨Òªº³q¥Î¦¡§ï¦¨ /^[a-zA-Z]\d{9}$/ ´N¥i¥H¤F¡I
- Yn¤ñ¹ï¼ÆÓ¦r¤¸¤¤ªº¥ô¤@Ó¦r¤¸¡A¥i¥Î¤¤¬A¸¹¡A¨Ã¥i¥Î¡u-¡v¨Ó¥Nªí¦r¥À©Î¬O¼Æ¦rªº½d³ò¡A¦]¦¹ [A-Z] ¥Nªí¥Ñ A ¦Ü Z ªº¥ô¤@Ó^¤å¦r¥À¡C¡]Y¤£¶û·Ð¡A·íµM¤]¥i¥H¼g¦¨ [ABCDEFGHIJKLMNOPQRSTUVWXYZ]¡C¡^
- \d ¥Nªí¥Ñ 0 ¦Ü 9 ªº¼Æ¥Ø¦r¡A¨Æ¹ê¤W¤]¥i¥H¼g¦¨ [0-9] ©Î [0123456789]¡C
- {9} ¥Nªí«e¤@Ó¦r¤¸ªº«½Æ¦¸¼Æ¡A¦]¦¹ \d{9} ¥Nªí»Ýn¦³¤EӼƥئr¡C
- ^ ¥Nªí¦r¦ê¶}©l¦ì¸m¡A$ ¥Nªí¦r¦êµ²§ô¦ì¸m¡C¡]Y¨S¦³³o¨âӲŸ¹¡A¨º»ò¥un¥ô¤@Ó¦r¦ê¤¤¶¡§t¦³¨¥÷ÃÒ¦r¸¹¡A¤]¥i¥H¤ñ¹ï¦¨¥\¡C¡^
¨Æ¹ê¤W¡A¨¥÷ÃÒ¦r¸¹¥»¨´N¦³¤º¦bªº½s½X³W«h¡A³o¨Ç³W«h©M¨Ï¥ÎªÌªº©Ê§O¦³Ãö¡A¦]¦¹Yn¹ê²{§¹¾ãªºªí³æÅçÃÒ¡A´N¥²¶·À³¥Î§¹¾ãªº¨¥÷ÃÒ½s½X³W«h¡AŪªÌ¥i°Ñ¦Ò¥»³¹ªº³Ì«á¤@¸`¡C
¥t¤@Ó²³æªº¨Ò¤l¡A¬On¨D¨Ï¥ÎªÌ¿é¤J«H¥Î¥d¸¹½X¡A³o¬O¤@²Õ 16 ӼƦrªº¸¹½X¡A¨Ò¦p¡G
·í§ÚÌ«ö¤U¡uÅçÃÒ¡v«ö¶s®É¡AJavaScript ·|©I¥s¨ç¼Æ checkCreditCard( ) ¨Ó¹ï¶ñ¤Jªº¸ê®Æ¶i¦æÅçÃÒ¡C¬ÛÃöì©l½X¦p¤U¡G
¦b¤W¨Ò¤¤¡A«ÜÅãµM¦a¡A/^\d{4}-\d{4}-\d{4}-\d{4}$/ ´N¥Nªí¥¿½Tªº«H¥Î¥d®æ¦¡¡C«Ü©úÅ㪺¡A¨Ï¥Î³q¥Î¦¡·|Åýµ{¦¡½X²¼ä«Ü¦h¡A¦Ó¥B·|¤j¤j´£°ªµ{¦¡½Xªº¥¿½T©Ê¡C¡]½Ð©M«e±³¹¸`ªºÃþ¦ü½d¨Ò formValidation02.htm ¤ñ¸û¬Ý¬Ý¡C¡^¦ýnª`·Nªº¬O¡A«H¥Î¥d¥d¸¹¥»¨´N¦³¤º¦bªº¸û½ÆÂø½s½X³W«h¡A¦]¦¹Yn¹ê²{§¹¾ãªºªí³æÅçÃÒ¡A´N¥²¶·À³¥Î§¹¾ãªº«H¥Î¥d¥d¸¹½s½X³W«h¡AŪªÌ¥i°Ñ¦Ò¥»³¹ªº³Ì«á¤@¸`¡C
¦pªG«½Æªº³¡¤À¦h©ó¤@Ó¦r¥À¡A§ÚÌ´N¥²¶·±N»Ýn«½Æªº³¡¤À©ñ¦b¤p¬A¸¹¤º¡A¦A¥[¤W¥Ñ¤j¬A¸¹¥]§¨ªº«½Æ¦¸¼Æ¡A¨Ò¦p¡A¤Wz½d¨Òªº³q¥Î¦¡ /^\d{4}-\d{4}-\d{4}-\d{4}$/¡A¤]¥i¥H¼g¦¨ /^(\d{4}-){3}\d{4}$/¡A½Ð¸Õ¸Õ¬Ý¡I
¤U¤@Ó¨Ò¤l¡A«h¬O¥Î³q¥Îªí¥Üªk¨ÓÅçÃҨϥΪ̪º^¤å¦W¦r¡A¨Ò¦p¡G
·í§ÚÌ«ö¤U¡uÅçÃÒ¡v«ö¶s®É¡AJavaScript ·|©I¥s¨ç¼Æ checkEnglishName( ) ¨Ó¹ï¶ñ¤Jªº¸ê®Æ¶i¦æÅçÃÒ¡C¬ÛÃöì©l½X¦p¤U¡G
¹ï©ó¤Wz½d¨Òµ{¦¡¡A§ÚÌ»¡©ú¦p¤U¡G
¦]¦¹ re1 = /^[A-Za-z\-]+\s+[A-Za-z\-]+$/ ¥i¥H¤ñ¹ï¥Ñ¨âÓ¦r·J©Ò§Î¦¨ªº^¤å¦W¦r¡A¨Ò¦p Michael Jordan¡F¦Ó re2 = /^[A-Za-z\-]+\s+[A-Za-z\-]+\s+[A-Za-z\-]+$/ «h¥i¥H¤ñ¹ï¥Ñ¤TÓ¦r·J©Ò§Î¦¨ªº^¤å¦W¦r¡A¨Ò¦p¡AJyh-Shing Roger Jang¡C
- [A-Za-z\-] ¥Nªí¤@Ó^¤å¦r¥À¡]¥i¥H¤j¼g©Î¤p¼g¡^¡A©Î¬O¦r¤¸¡u-¡v¡C¯S§Onª`·Nªº¬O¡A¥Ñ©ó¡u-¡v¦b¤¤¬A¸¹¤º³¡¤w¸g¦³¯S®í·N¸q¡AYnÁ×±¼¦¹¯S®í·N¸q¡A´N¥²¶·¦b¡u-¡v¤§«e¥[¤W¤Ï±×½u¡]¡u\¡v¡^¡C
- ¥[¸¹¥Nªí«½Æ«e¤@Ó¦r¤¸¤@¦¸©Î¦h¦¸¡A¦]¦¹ [A-Za-z\-]+ ´N¥Nªí¥Ñ^¤å¦r¥À©Î¬O´î¸¹©Ò§Î¦¨ªº¦r¦ê¡A¥B¨äªø«×¦Ü¤Ö¬O¤@¡C
- \s ¥NªíªÅ¥Õ¦r¤¸¡A¥i¥H¬OªÅ®æ¡B©w¦ìÁä¡B´«¦C¦r¤¸µ¥µ¥¡C¦]¦¹ \s+ ´Nªí¥Ü¥Ñ¤@өΦhӪťզr¤¸©Ò§Î¦¨ªº¦r¦ê¡C
¤U¤@Ó¨Ò¤l¡A«h¬O¥Î³q¥Îªí¥Üªk¨ÓÅçÃÒ¹q¤l¶l¥ó¡A¨Ò¦p¡G
·í§ÚÌ«ö¤U¡uÅçÃÒ¡v«ö¶s®É¡AJavaScript ·|©I¥s¨ç¼Æ checkEmail( ) ¨Ó¹ï¶ñ¤Jªº¸ê®Æ¶i¦æÅçÃÒ¡C¬ÛÃöì©l½X¦p¤U¡G
¹ï©ó¦¹½d¨Ò©Ò¥Î¨ìªº³q¥Î¦¡ /^.+@.+\..{2,3}$/¡A»¡©ú¦p¤U¡G
¦]¦¹ /^.+@.+\..{2,3}$/ ¥i¥Î¨Ó¤ñ¹ï¤@¯ëªº email ±b¸¹¡A¨Ò¦p test@cs.nthu.edu.tw ©Î¬O roger_jang@mathworks.com µ¥¡A¦ý¬O¦¹³q¥Î¦¡¨Ã«Dºw¤ô¤£º|¡A¦³¨Ç¤£¦X®æªº email ±b¸¹¤]·|¤ñ¹ï¦¨¥\¡A¨Ò¦p " @math.com"¡A©Î¬O "test@ .tw"¡A©Î¬O "aa@bb.zz"¡CYnÁ׶}§t¦³ªÅ¥Õªº email ±b¸¹¡A½Ð¨£¤U¦C½d¨Ò¡G
- ¥y¸¹¥i¥H¤ñ¹ï¥ô¤@Ó¦r¤¸¡]¦ý¤£¥]§t´«¦C¦r¤¸¡^¡A¦]¦¹ .+ ¥Nªíªø«×¤£¬°¹sªº¦r¦ê¡C
- Yn¤ñ¹ï¡u.¡v¥»¨¡A¥Ñ©ó¨äì¥ý¤w¸g¨ã¦³¯S®í·N¸q¡A©Ò¥H¥²¶·¥[¤W¤Ï±×½u¡G¡u\.¡v¡C
- .{2,3} ¥Nªíªø«×¬° 2 ©Î 3 ªº¦r¤¸¡A¥i¥H¤ñ¹ï°ê½X¡]¨âÓ¦r¤¸¡^©Î¬O com¡Bedu µ¥¬ü°ê¦a°Ï±M¥Î½X¡C
- ¦A¦¸»¡©ú¡G^ ¥Nªí¦r¦ê¶}©l¦ì¸m¡A$ ¥Nªí¦r¦êµ²§ô¦ì¸m¡C
¬ÛÃöì©l½X¦p¤U¡G
¹ï©ó¦¹½d¨Ò©Ò¥Î¨ìªº³q¥Î¦¡ /^[^\s]+@[^\s]+\.[^\s]{2,3}$/¡A»¡©ú¦p¤U¡G
- \s ¥Nªí©Ò¦³¥i¯àªºªÅ¥Õ¦r¤¸¡A¥]§tªÅ¥Õ¡B©w¦ìÁä¡B´«¦C¦r¤¸µ¥¡C¡]¦ý¨Ã¤£¥]§t¥þ§ÎªºªÅ¥Õ¡A½Ð¯S§Oª`·N¡I¡^
- ^ ¦b¤¤¬A©·¤º¬O¥Nªí¡u§_©w¡v¡A¦]¦¹ [^\s]+ ¥Nªí¡u¥Ñ¤@өΦhÓ«DªÅ¥Õ¦r¤¸¡v©Ò§Î¦¨ªº¦r¦ê¡C
- Yn¹ï¹q¶l¶i¦æ§óÄYVªº¤ñ¹ï¡A¤Wz³q¥Î¦¡¥i¥H§ï¦¨ /^\w+@\w+\.\w{2,3}$/¡A¨ä¤¤ \w ¥Nªí¤@ӼƦr¡B¦r¥À©Î©³½u¡A¥þµ¥©ó [a-zA-Z0-9_]¡C¡]¦ý³o¼ËÄYVªº¼Ð·Ç¤]¥i¯à¿ù»~¦aªý¾×¥¿±`ªº¹q¶l¡C¡^
¦b¥H¤Uªº½d¨Ò¤¤¡A§Ú̳]p¤F¤@Óªí³æ¡A¥i¥HÅý¨Ï¥ÎªÌ¿é¤J¥ô·N¦r¦ê¡B³q¥Î¦¡¡A¥H¤Î¤ñ¹ï¿ï¶µ¡A¨Ã¦b³q¥Î¦¡¤ñ¹ï«á¡A¦C¥X¤ñ¹ï¨ìªº¦r¦ê¡AŪªÌÌ¥i¥H§Q¥Î¦¹½d¨Ò¡A¤ÏÂкt½m¡A¥H¼W¶i¹ï©ó³q¥Î¦¡ªºÁA¸Ñ¡G
¤Wz½d¨Òªºì©lÀɦp¤U¡G
¦b¤Wz½d¨Ò¤¤¡A§Ų́ϥΤF¦r¦êªº match() ¤èªk¡A¨Ó¹ï³q¥Î¦¡¶i¦æ¤ñ¹ï¡A¦]¦¹ matched = str.match(regexp) ¥i±N¤ñ¹ï¨ìªº¦r¦ê°e¨ì¤@Ó°}¦C¡A¥H«K«áÄò³B²z¡C
¦b¶i¦æªí³æ¸ê®ÆÅçÃÒ¤§«e¡A§ÚÌÀ³¥ý¶i¦æªí³æ¸ê®Æקï¡A¨Ò¦p®³±¼¤£¥²nªºªÅ®æ¡B^¤å¦r¥À¤j¤p¼gÂà´«µ¥¡A³o¨Ç¤u§@¤]¥i¥H¥Ñ¦r¦êªº replace() ¤èªk©Î³q¥Î¦¡ªº exec() ¤èªk¨Ó¹F¦¨¡A³o¬O§Ṳ́U¤@¸`ªº¥DÃD¡C
JavaScript µ{¦¡³]p»PÀ³¥Î¡G¥Î©óºô¶¥Î¤áºÝ