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

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! "


Ïà¹ØÎĵµ£º

JavaScript ¶ÁÈ¡ JSON


JSON:
[{"subjecttypeId":"1","subjectName":"67","scope":"1",},{"subjecttypeId":"1","subjectName":"345","scope":"1"}]
JavaScript:
       var result = window.eval('(' + JSON+ ')');

for(var p in result){

result[p].subjectName;

alert(result[p] ......

Javascript ʹÓÃIDE¹¤¾ß½øÐе÷ÊÔ

1. Javascript Debug Toolkit½éÉÜ
JSDT(Javascript Debug Toolkit)ÊÇÒ»¸öÓÃÓÚjavascriptµ÷ÊÔµÄeclipse
²å¼þ£¬ÓÃÓÚµ÷ÊÔjavascript¡£JSDT¿ÉÒÔ¿çä¯ÀÀÆ÷µ÷ÊÔ£¬Ö§³ÖÔÚIE,Firefox,Safari,ChromeµÈÖ÷Á÷ä¯ÀÀÆ÷Öе÷ÊÔ
javascript¡£JSDTÖ§³ÖÉèÖöϵ㣬µ¥²½µ÷ÊԵȵ÷ÊÔ¹¤¾ßµÄ»ù±¾ÌØÐÔ¡£
2. ÈçºÎ°²×°
JSDTÊÇ»ùÓÚeclipse3.2+,jdk1.5+ ......

javascript String.replaceº¯Êý

    javascriptµÄStringÀàÄÚÖú¯Êýreplace(regexp, newString)º¯ÊýÌṩÁË×Ö·û´®Ìæ»»¹¦ÄÜ£¬´Óº¯ÊýÔ­ÐÍÉÏ¿ÉÒÔ¿´³öÖ§³Ö Regular Exp¡£´Ëº¯Êý¹¦Äܷdz£ÊµÓ㬵«Ò²Óм¸¸öµØ·½Óеã»ìÏý£¬ÏÂÃæͨ¹ýʵ¼ÊµÄÀý×ÓÀ´ËµÃ÷£º
    ±ÈÈçÎÒÃÇÏÖÔÚÒªÕë¶Ô´®
var src="<a href=&quot;http://xx.com/a/2010 ......

ÖÊÊý Javascript ֮ǰµÄÄǸöÓеãС벡

<mce:script type="text/javascript"><!--
document.write("<table border=1px border-color=red>"); //±í¸ñ¿ªÊ¼
for (h = 1; h <= 10; h++) {
document.write("<tr>"); //¿ªÊ¼µÚÒ»ÐÐ
for (s = 1; s <= 10; s++) {
var num = (h-1)*10+s; ......

javascriptдÔÚº¯ÊýÀïÃæ

javascriptдÔÚº¯ÊýÀïÃæ
function loadscript(url) {
document.write('<scr' + 'ipt  type="text/javascript" src"' + url + '"><\/scr' + 'ipt>'>)
}
Ôڳɹ¦µÄÕýÔò±í´ïʽ²éÕÒÍê³É֮ǰ£¬ÆäÊôÐÔֵΪ undefined¡£
²»Òª½«È«¾Ö RegExp ¶ÔÏóÓë ÕýÔò±í´ïʽ
¶ÔÏó»ìÏý¡£¾¡¹ÜÌýÆðÀ´Ï ......
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØͼ | ¸ÓICP±¸09004571ºÅ