JavaScript replace() ·½·¨
JavaScript replace() ·½·¨
JavaScript String ¶ÔÏó²Î¿¼ÊÖ²á
¶¨ÒåºÍÓ÷¨
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! "
str=str + "We are proud to announce that Microsoft has "
str
Ïà¹ØÎĵµ£º
ä¯ÀÀÆ÷£ºIE 8¡¢FF 3.6¡¢Chrome 4.0¡¢Safari 4.0¡¢Opera 10.1
½öÓÐIEä¯ÀÀÆ÷Ö§³ÖHTMLElement.onresize£¨±ÈÈçbody.onresize£©
ÆäËüä¯ÀÀÆ÷Ö»Ö§³Öwindow.onresize
ÏÈ˵IEµÄHTMLElement.onresize
ʹÓÃÇ°ÇëÈ·¶¨ÄãµÄÐÄÔ༰ѪѹÕý³££¬Èç¹ûÄ㶨ÒåÁË
body.onresize = function(){……}»òÕßhtml.onresize = obj.fun;
......
//¸´ÖÆËùÑ¡±í¸ñµÄÊý¾Ýµ½¼ôÌù°å ×÷Õߣºxx
function copyData(){
var content = "";
var tbl = getTbl();
//»ñÈ¡ËùÑ¡µÄËùÓÐÊý¾Ý
for(j = start_Row; j <= end_Row; j++)
{
for(i = start_Col; i <= end_Col; i++)
{
content += (tbl.rows[j].cells[i].childNodes[0].value);
if(i != end_ ......
JavaScript ¿ÉËãÊÇÊÀ½çÉÏ×îÁ÷Ðеıà³ÌÓïÑÔ£¬ËüÔø±» Web ¿ª·¢Éè¼ÆʦÌùÉÏجÃεıêÇ©£¬ËäÈ»ÕæÕýµÄجÃÎÆäʵÊÇ DOM API£¬Õâ¸ö±»´óÁ¿µÄ¿ª·¢ÓëÉè¼ÆʦËæÊÖÄéÀ´ÔöÇ¿ËûÃÇµÄ Web Ç°¶ËµÄ½Å±¾ÓïÑÔ£¬Èç½ñÔ½À´Ô½±»ÖØÊÓ£¬ËäÔòÈç´Ë£¬JavaScript ÈÔȻӵÓкܶàÈÃÈ˷ѽâµÄ¶«Î÷¡£
1. ËüÒÔ Java ÃüÃû£¬µ«²¢²»ÊÇ Java
Ëü×î³õ½Ð Mocha£¬ ½ÓןÄÃûÎ ......
ÏÖÔڸоõjsºÜºÃºÜÇ¿´ó£¬Ëæ×ÅÉîÈëµÄѧϰ£¬Äã¾Í»á²»»á¾Ü¾øÔÚ¿Í»§¶ËʹÓÃjs¡£Ö®Ç°Ò²ÔÚÍøÉÏÕÒÁ˵ã×ÊÁÏ£¬Ò»Æð¿´¿´¡£
value="ÎÒÊÇ button" />
¶¯Ì¬Ìí¼Óonclickʼþ£º
<input type="button" value="ÎÒÊÇ button" id="bu">
<script type="text/javascript">
var bObj=document. ......