[·Òë]High Performance JavaScript(016)
Regular Expression Optimization ÕýÔò±í´ïʽÓÅ»¯
Incautiously crafted regexes can be a major performance bottleneck (the upcoming section, "Runaway Backtracking" on page 91, contains several examples showing how severe this can be), but there is a lot you can do to improve regex efficiency. Just because two regexes match the same text doesn't mean they do so at the same speed.
´ÖdzµØ±àдÕýÔò±í´ïʽÊÇÔì³ÉÐÔÄÜÆ¿¾±µÄÖ÷ÒªÔÒò£¨ºóÃæ“»ØËÝʧ¿Ø”Ò»½ÚÓÐһЩÀý×Ó˵Ã÷ÕâÊǶàôÑÏÖصÄÎÊÌ⣩£¬µ«»¹Óкܶà¿ÉÒԸĽøÕýÔò±í´ïʽЧÂʵĵط½¡£Á½¸öÕýÔò±í´ïʽƥÅäÏàͬµÄÎı¾²¢²»Òâζ×ÅËûÃǾßÓÐͬµÈµÄËٶȡ£
Many factors affect a regex's efficiency. For starters, the text a regex is applied to makes a big difference because regexes spend more time on partial matches than obvious nonmatches. Each browser's regex engine also has different internal optimizations.
Ðí¶àÒòËØÓ°ÏìÕýÔò±í´ïʽµÄЧÂÊ¡£Ê×ÏÈ£¬ÕýÔò±í´ïʽÊÊÅäµÄÎı¾Ç§²îÍò±ð£¬²¿·ÖÆ¥Åäʱ±ÈÍêÈ«²»Æ¥ÅäËùÓõÄʱ¼äÒª³¤¡£Ã¿ÖÖä¯ÀÀÆ÷µÄÕýÔò±í´ïʽÒýÇæÒ²Óв»Í¬µÄÄÚ²¿ÓÅ»¯¡£
Regex optimization is a fairly broad and nuanced topic. There's only so much that can be covered in this section, but what's included should put you well on your way to understanding the kinds of issues that affect regex performance and mastering the art of crafting efficient regexes.
ÕýÔò±í´ïʽµÄÓÅ»¯ÊÇÒ»¸öÏ൱¹ã·ººÍϸÖÂÈë΢µÄ»°Ìâ¡£±¾½ÚÌÖÂÛ¾¡ÆäËùÄÜ£¬Ï£ÍûÕâЩÄÚÈÝÓÐÖúÓÚÄúÀí½âÓ°ÏìÕýÔò±í´ïʽÐÔÄܵĸ÷ÖÖÎÊÌâºÍÕÆÎÕ±àд¸ßЧÕýÔò±í´ïʽµÄÒÕÊõ¡£
Note that this section assumes you already have some experience with regular expressions and are primarily interested in how to make them faster. If you're new to regular expressions or need to brush up on the basics, numerous resources are available on the Web and in print. Regular Expressions Cookbook (O'Reilly) by Jan Goyvaerts and Steven Levithan (that's me!) is written for people who like to learn by doing, and
Ïà¹ØÎĵµ£º
±¾ÎÄÖ÷ÒªÊdzöÓÚÓÐÅóÓÑʹÓÃÎÒÔÀ´Ð´µÄautocompleteµÄJS¿Ø¼þ¡£µ±Êý¾ÝÁ¿´óµÄʱºò£¬»á³öÏÖЧÂʼ«ÆäÂýµÄÇé¿ö£¬ÎÒÔÚÕâ¶Îʱ¼ä×ö³öµÄһЩ²âÊÔÒ²¼°Ò»Ð©¾Ñ飬Óë´ó¼Ò·ÖÏí£¬Èç¹ûÓдíµÄµØ·½£¬»¹ÇëÖ¸³ö¡£
¾¹ý²âÊÔ£¬ÎÒÃǻᷢÏÖÈçϵÄÇé¿ö»òÕß˵µÄ½áÂÛ£¬Èç¹ûÄúµÄ²âÊÔ½á¹ûÓëÎҵIJ»·û£¬Çë˵Ã÷ÔÒò£¬ÒÔ±ãÏ໥ѧϰ¡£
1£©µ±Ò»¸ö½Ï´óµÄHTML×Ö· ......
<script>
//дcookiesº¯Êý ×÷ÕߣºµÔÕñ¿
function
SetCookie(name,value)//Á½¸ö²ÎÊý£¬Ò»¸öÊÇcookieµÄÃû×Ó£¬Ò»¸öÊÇÖµ
{
var Days = 30;
//´Ë cookie ½«±»±£´æ 30 Ìì
var exp = new Date(); //new
Date("December 31, 9998");
  ......
JavascriptÊý¾ÝÀàÐÍ
ÓÉÓÚjavascriptÊÇÈõÀàÐÍÓïÑÔ£¬¼´¶¨Òå±äÁ¿Ê±²»±ØÉùÃ÷ÆäÀàÐÍ
¡£µ«Õâ²¢²»Òâζ×űäÁ¿Ã»ÓÐÀàÐÍ¡£ÒòΪ¸³ÖµÊ±»á×Ô¶¯Æ¥ÅäÊý¾ÝÀàÐÍ£¡
Ä¿Ç°Óõ½µÄ»ù±¾Êý¾ÝÀàÐÍ
number
boolean
string
var i
i="test";//Õâʱi¾Í³ÉÁËstringÀàÐÍ
var i
i=4;//Õâʱi¾Í³ÉÁËnumberÀàÐÍ
³£ÓöÔÏóÀàÐÍ<object> ......
ÍæPHP¡¢Delphi¡¢Java»ù±¾É϶¼ÓжÔÏó£¬Ï°¹ßÕâÖÖ˼·ºóÉÏÊÖÈκÎÓïÑÔ¶¼Ïë¿¿OO˼·£¬Õâ¾ø²»ÊÇÔÚ¸Ïʱ÷Ö£¬¶øÊÇ°ÑÏà¹Ø´úÂë½øÐÐÄÚ¾ÛµÄÈ·¿ÉÒÔÌå»áµ½Î¬»¤µÄ·½±ã£¡
ÔÚJavaScriptÖÐÈçºÎ´´½¨¶ÔÏó£¿
JavaScriptÊÇ»ùÓÚ¶ÔÏóµÄ£¡ËüÒ²ÊÇÒÔObjectΪ¸ùÀ࣬ÆäËüÀà¼Ì³ÐÖ®¡£ÔÚ¸ùÀàÌṩÁ˼¸¸ö·½·¨¡£¹©¼Ì³ÐÀàʹÓã¡
ÒÔÏÂÊÇ´´½¨¶ÔÏóµÄÀý×Ó£º
funct ......
typeof ÊÇJavaScriptµÄÔËËã·û
——·µ»ØÒ»¸öÓÃÀ´±íʾ±í´ïʽµÄÊý¾ÝÀàÐ͵Ä×Ö·û´®
typeof[(] expression [)] //typeofÓï·¨ÖеÄÔ²À¨ºÅÊÇ¿ÉÑ¡Ïî
typeof ÔËËã·û@import url(../html-vss/msdnie4a.css);
typeof
ÔËËã·û°ÑÀàÐÍÐÅÏ¢µ±×÷×Ö·û´®·µ»Ø¡£typeof
·µ»ØÖµÓÐÁ ......