JavaScript replace() ·½·¨
¶¨ÒåºÍÓ÷¨
replace() ·½·¨ÓÃÓÚÔÚ×Ö·û´®ÖÐÓÃһЩ×Ö·ûÌæ»»ÁíһЩ×Ö·û£¬»òÌæ»»Ò»¸öÓëÕýÔò±í´ïʽƥÅäµÄ×Ó´®¡£
Óï·¨
stringObject.replace(regexp,replacement)
²ÎÊý
ÃèÊö
regexp
±ØÐè¡£¹æ¶¨ÁËÒªÌæ»»µÄģʽµÄ RegExp ¶ÔÏó¡£Çë×¢Ò⣬Èç¹û¸ÃÖµÊÇÒ»¸ö×Ö·û´®£¬Ôò½«Ëü×÷ΪҪ¼ìË÷µÄÖ±½ÓÁ¿Îı¾Ä£Ê½£¬¶ø²»ÊÇÊ×Ïȱ»×ª»»Îª RegExp ¶ÔÏó¡£
replacement
±ØÐè¡£Ò»¸ö×Ö·û´®Öµ¡£¹æ¶¨ÁËÌæ»»Îı¾»òÉú³ÉÌæ»»Îı¾µÄº¯Êý¡£
·µ»ØÖµ
Ò»¸öеÄ×Ö·û´®£¬ÊÇÓà replacement Ìæ»»ÁË regexp µÄµÚÒ»´ÎÆ¥Åä»òËùÓÐÆ¥ÅäÖ®ºóµÃµ½µÄ¡£
˵Ã÷
×Ö·û´® stringObject µÄ replace() ·½·¨Ö´ÐеÄÊDzéÕÒ²¢Ìæ»»µÄ²Ù×÷¡£Ëü½«ÔÚ stringObject ÖвéÕÒÓë
regexp ÏàÆ¥ÅäµÄ×Ó×Ö·û´®£¬È»ºóÓà replacement À´Ìæ»»ÕâЩ×Ó´®¡£Èç¹û regexp ¾ßÓÐÈ«¾Ö±êÖ¾ g£¬ÄÇô replace()
·½·¨½«Ìæ»»ËùÓÐÆ¥ÅäµÄ×Ó´®¡£·ñÔò£¬ËüÖ»Ìæ»»µÚÒ»¸öÆ¥Åä×Ó´®¡£
replacement ¿ÉÒÔÊÇ×Ö·û´®£¬Ò²¿ÉÒÔÊǺ¯Êý¡£Èç¹ûËüÊÇ×Ö·û´®£¬ÄÇôûÓÐÆ¥Å䶼½«ÓÉ×Ö·û´®Ìæ»»¡£µ«ÊÇ replacement ÖÐµÄ $ ×Ö·û¾ßÓÐÌض¨µÄº¬Òå¡£ÈçϱíËùʾ£¬Ëü˵Ã÷´ÓģʽƥÅäµÃµ½µÄ×Ö·û´®½«ÓÃÓÚÌæ»»¡£
×Ö·û
Ìæ»»Îı¾
$1¡¢$2¡¢...¡¢$99
Óë regexp ÖÐµÄµÚ 1 µ½µÚ 99 ¸ö×Ó±í´ïʽÏàÆ¥ÅäµÄÎı¾¡£
$&
Óë regexp ÏàÆ¥ÅäµÄ×Ó´®¡£
$`
λÓÚÆ¥Åä×Ó´®×ó²àµÄÎı¾¡£
$'
λÓÚÆ¥Åä×Ó´®ÓÒ²àµÄÎı¾¡£
$$
Ö±½ÓÁ¿·ûºÅ¡£
×¢Ò⣺
ECMAScript v3 ¹æ¶¨£¬replace() ·½·¨µÄ²ÎÊý
replacement
¿ÉÒÔÊǺ¯Êý¶ø²»ÊÇ×Ö·û´®¡£ÔÚÕâÖÖÇé¿öÏ£¬Ã¿¸öÆ¥Å䶼µ÷Óøú¯Êý£¬Ëü·µ»ØµÄ×Ö·û´®½«×÷ΪÌæ»»Îı¾Ê¹Ó᣸ú¯ÊýµÄµÚÒ»¸ö²ÎÊýÊÇÆ¥ÅäģʽµÄ×Ö·û´®¡£½ÓÏÂÀ´µÄ²ÎÊý
ÊÇÓëģʽÖеÄ×Ó±í´ïʽƥÅäµÄ×Ö·û´®£¬¿ÉÒÔÓÐ 0 ¸ö»ò¶à¸öÕâÑùµÄ²ÎÊý¡£½ÓÏÂÀ´µÄ²ÎÊýÊÇÒ»¸öÕûÊý£¬ÉùÃ÷ÁËÆ¥ÅäÔÚ stringObject
ÖгöÏÖµÄλÖá£×îºóÒ»¸ö²ÎÊýÊÇ stringObject ±¾Éí¡£
ʵÀý
Àý×Ó 1
ÔÚ±¾ÀýÖУ¬ÎÒÃǽ«Ê¹Óà "W3School" Ìæ»»×Ö·û´®ÖÐµÄ "Microsoft"£º
<script type="text/javascript">
var str="Visit Microsoft!"
document.write(str.replace(/Microsoft/, "W3School")
)
</script>
Êä³ö£º
Visit W3School!
Àý×Ó 2
ÔÚ±¾ÀýÖУ¬ÎÒÃǽ«Ö´ÐÐÒ»´ÎÈ«¾ÖÌæ»»£¬Ã¿µ± "Microsoft" ±»ÕÒµ½£¬Ëü¾Í±»Ì滻Ϊ "W3School"£º
<script type="text/javascript">
var str="Welcome to Microsoft! "
Ïà¹ØÎĵµ£º
Summary ×ܽá
When web pages or applications begin to feel slow, analyzing assets as they come over the wire and profiling scripts while they are running allows you to focus your optimization efforts where they are needed most.
µ±ÍøÒ³»òÓ¦ÓóÌÐò±äÂýʱ£¬·ÖÎ ......
±¾ÎļòÒª½éÉÜJavaScriptÖÐconfirm,alert,promptµÄÓ÷¨,Ï£Íû´ó¼Òѧϰ¹ý³ÌÖÐÄܵõ½Ò»Ð©Æô·¢¡£
window.confirm ²ÎÊý¾ÍÖ»ÓÐÒ»¸ö¡£ÏÔʾÌáʾ¿òµÄÐÅÏ¢¡£°´È·¶¨,·µ»Øtrue£»°´È¡Ïû·µ»Øfalse¡£
< SCRIPT> var bln = window.confirm("È·¶¨Âð?"); alert(bln) < /SCRIPT> window.alert²ÎÊý£¬Ö»ÓÐ ......
¡ö´ò¿ª¡ö
<input name=Button onClick=document.all.WebBrowser.ExecWB(1,1) type=button value=´ò¿ª>
<OBJECT classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 height=0 id=WebBrowser width=0></OBJECT>
¡öÁí´æΪ¡ö
<input name=Button onClick=document.all.WebBrowser.ExecWB(4,1) typ ......
JavaScriptµÄä¯ÀÀÆ÷¶ÔÏóÏê½â
Ò»¡¢ä¯ÀÀÆ÷¶ÔÏó²ã´Î¼°ÆäÖ÷Òª×÷ÓÃ
¶þ¡¢Îĵµ¶ÔÏó¹¦Äܼ°Æä×÷ÓÃ
Èý¡¢·¶Àý
ʹÓÃä¯ÀÀÆ÷µÄÄÚ²¿¶ÔÏóϵͳ, ¿ÉʵÏÖÓëHTMLÎĵµ½øÐн»»¥¡£ËüµÄ×÷ÓÃÊǽ«Ïà¹ØÔªËØ×éÖ¯°ü×°ÆðÀ´£¬Ìṩ¸ø³ÌÐòÉè¼ÆÈËԱʹÓ㬴Ӷø¼õÇá±à³ÌÈ˵ÄÀͶ¯£¬Ìá¸ßÉè¼ÆWebÒ³ÃæµÄÄÜÁ¦¡£
Ò»¡¢ä¯ÀÀÆ÷¶ÔÏó²ã´Î¼°ÆäÖ ......