Ò׽ؽØͼÈí¼þ¡¢µ¥Îļþ¡¢Ãâ°²×°¡¢´¿ÂÌÉ«¡¢½ö160KB

JavaScript ÓÃÓÚģʽƥÅäµÄ RegExp ·½·¨

±¾ÆªÈÕÖ¾ÏÈÖ÷ÒªÌÖÂÛ RegExp ¶ÔÏóµÄ²¿·Ö·½·¨£¬RegExp ¶ÔÏó¶¨ÒåÁËÁ½¸öÓÃÓÚģʽƥÅäµÄ·½·¨£¬ËüÃÇÊÇexec()
ºÍtest()
ÏÂÃæÎÒÃÇÖðÒ»½øÐн²½â
¡£ºÇ£¬ºÍÉÏƪÈÕÖ¾Ò»ÑùµÄ½éÉÜ£¬¿´À´Ì×»°£¬¹ûÈ»ÊÊÓÃÓÚ¶à¸ö³¡ºÏ¡£àÅ¡£
exec() ¼ìË÷×Ö·û´®ÖеÄÖ¸¶¨Öµ£¬·µ»ØÖµÊDZ»ÕÒµ½µÄÖµ£¬Èç¹ûûÓз¢ÏÖÆ¥Å䣬Ôò·µ»Ø null£º
Ö÷ÒªµÄRegExpģʽƥÅä·½·¨ÊÇexec()£¬ÆäÓëÉÏһƪ½éÉܵÄString¶ÔÏóµÄmatch·½·¨ÏàËÆ£¬Ö»²»¹ýËüÊÇÒÔ×Ö·û´®Îª²ÎÊýµÄRegExp·½·¨£¬¶ø²»ÊÇÒÔRegExp¶ÔÏóΪ²ÎÊýµÄ×Ö·û´®·½·¨¡£ºô£¬ÕâÒ»¾äÓеãÏñÈÆ¿ÚÁû°ì·¨£¬ÊéÉϵÄÔ­»°£¬³­¹ýÀ´Èôó¼ÒÀÖÒ»ÀÖ£¬ºÇ¡£exec()·½·¨ÔÚÒ»¸ö×Ö·û´®ÖмìË÷Æ¥Å䣬Èç¹ûûÓÐÕÒµ½ÈκÎÆ¥Åä·µ»Ønull£¬Èç¹ûËüÕÒµ½ÁËÒ»¸öÆ¥Å䣬½«·µ»ØÒ»¸öÊý×é¡£
¾ÍÏñ·½·¨match()Ϊ·ÇÈ«¾Ö¼ìË÷·µ»ØµÄÊý×éÒ»Ñù¡£Õâ¸öÊý×éµÄÔªËØ0°üº¬µÄÊÇÓëÕýÔò±í´ïʽÏàÆ¥ÅäµÄ×Ö·û´®£¬ÓàϵÄËùÓÐÔªËØ°üº¬µÄÊÇÓë×Ó±í´ïʽÏàÆ¥ÅäµÄ×Ó´®£¬¶øÇÒÊôÐÔindex°üº¬ÁËÆ¥Åä·¢ÉúµÄ×Ö·ûµÄλÖã¬ÊôÐÔinputÒýÓõÄÊDZ»¼ìË÷µÄ×Ö·û´®¡£
/*
i is 0  http://www.xiaoxiaozi.com/index.php
i is 1  http
i is 2  www.xiaoxiaozi.com
i is 3  index.php
i is index  11
i is input  my home is http://www.xiaoxiaozi.com/index.php
*/
var
str =
'my home is http://www.xiaoxiaozi.com/index.php'
;
var
pattern =
/(\w+):\/\/([\w.]+)\/(\S*)/
;
//var result = str.match(pattern);
var
result =
pattern.exec
(
str)
;
for
(
i in
result)
{
        document.write
(
"i is "
+
i+
"  "
+
result[
i]
+
"<br/>"
)
;
}
Óëmatch()·½·¨²»Í¬µÄÊÇ£¬exec()·µ»ØµÄÊý×éÀàÐͲ»Í¬£¬ÎÞÂÛ¸ÃÕýÔò±í´ïʽÊÇ·ñº¬ÓÐÈ«¾ÖÊôÐÔg¡£ÒòΪµ±º¬ÓÐÈ«¾ÖÊôÐÔgµÄʱ
ºò£¬match()·½·¨·µ»ØµÄÊÇÒ»¸öÆ¥ÅäµÄÊý×飬¶øµ÷ÓÃexec()·½·¨µÄRegExpÓÐÒ»¸ölastIndexÊôÐÔ£¬Ëü½«°Ñ¸Ã¶ÔÏóµÄlastIndex
ÊôÐÔÉèÖõ½½ô½Ó×ÅÆ¥Åä×Ó´®µÄ×Ö·ûλÖá£
µ±Í¬Ò»¸öRegExp¶ÔÏóµÚ¶þ´Îµ÷ÓÃexec()ʱ£¬Ëü½«´ÓlastIndexÊôÐÔËùָʾµÄ×Ö·û´¦¿ªÊ¼¼ìË÷£¬Èç¹ûexec()ûÓз¢ÏÖÈκÎÆ¥Å䣬Ëü
»á½«lastIndexÊôÐÔÖØΧΪ0£¬(ÔÚÈκÎʱºòÄ㶼¿ÉÒÔ½«lastIndexÊôÐÔÉèΪ0£¬Ã¿µ±ÄãÔÚÒ»¸ö×Ö·û´®ÖÐÕÒµ½×îºóÒ»¸öÆ¥Åä֮ǰ¾Í¿ªÊ¼ÓÃͬһ¸ö
RegExp¶ÔÏóÀ´¼ìË÷ÁíÒ»¸ö×Ö·û´®£¬¶ø·ÅÆúÁËÔ­À´µÄ¼ìË÷µÄʱºò¾Í·½·¨Ó¦¸ÃÕâÑù×ö)¡£ÕâÒ»ÌØÊâµÄÐÐΪʹ


Ïà¹ØÎĵµ£º

JavaScript FAQ£¨¶þÊ®Èý£©

¶þÊ®¡¢Bookmarklet
1¡¢Ê²Ã´ÊÇBookmarklet£¿£¨What's a Bookmarklet?£©
Q£ºÊ²Ã´ÊÇBookmarklet£¿
A£ºBookmarkletÊÇÕû¸ö¶¼±»°üº¬ÔÚ³¬Á´½ÓURLÖеÄһС¶ÎJavaScript³ÌÐò¡££¨JavaScript URL¾ÍÊÇÕâ¸öÑù×Ó£º<a href="javascript:the code goes here">¡££©¶àÊýä¯ÀÀÆ÷ÔÊÐíÓû§Ìí¼ÓÕâЩJavaScript URLÊéÇ©£¬¾ÍÏñÌí¼ÓÆäËû³¬Á´½ ......

JavaScriptÅжÏä¯ÀÀÆ÷ÀàÐͼ°°æ±¾

      ÄãÖªµÀÊÀ½çÉÏÓжàÉÙÖÖä¯ÀÀÆ÷Â𣿳ýÁËÎÒÃÇÊìÖªµÄIE, Firefox, Opera, SafariËÄ´óä¯ÀÀÆ÷Ö®Í⣬ÊÀ½çÉÏ»¹Óнü°ÙÖÖä¯ÀÀÆ÷¡£
       ¼¸ÌìÇ°£¬ä¯ÀÀÆ÷¼Ò×åÓиյ®ÉúÁËһλСÍõ×Ó£¬¾ÍÊÇGoogleÍƳöµÄChromeä¯ÀÀÆ÷¡£ÓÉÓÚChrome³öÉúÃûÃÅ£¬¾¡¹ÜËû»¹ÊǸöС¼Ò»ï£¬Ã»ÓÐÈ˸ÒС ......

javascript Öеļ̳з½·¨

1.¿ÉÒÔͨ¹ýprototypeÊôÐÔ£¬ÊµÏּ̳з½·¨µÄ·½Ê½£¬ÕâÖÖ·½Ê½¾ÍÊÇjavaÓïÑÔÖм̳еı任ÐÎʽ¡£
 // Create the constructor for a Person object
function Person( name ) {
    this.name = name;
}
// Add a new method to the Person object
Person.prototype.getName = function() {
   ......
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØͼ | ¸ÓICP±¸09004571ºÅ