javascriptÖÐreplaceÕýÔò±í´ïʽÓï·¨
replace()×î¼òµ¥µÄËãÊÇÄÜÁ¦¾ÍÊǼòµ¥µÄ×Ö·ûÌæ»»¡£
ʾÀý´úÂëÈçÏ£º
<script language="javascript">
var strM = "javascript is a good script language";
//ÔÚ´ËÎÒÏ뽫×ÖĸaÌæ»»³É×ÖĸA
alert(strM.replace("a","A"));
</script>
ËüÖ»Ìæ»»ÁËÊ××Öĸ¡£µ«Èç¹û¼ÓÉÏÕýÔò±í´ïʽ½á¹û¾Í²»Ò»ÑùÁË£¡replace()Ö§³ÖÕýÔò±í´ïʽ£¬Ëü¿ÉÒÔ°´ÕÕÕýÔò±í´ïʽµÄ¹æÔòÆ¥Åä×Ö·û»ò×Ö·û´®£¬È»ºó¸øÓèÌæ»»£¡
×¢Ò⣺±»Ìæ»»µÄ²¿·Ö²»ÓüÓË«ÒýºÅ.
<script language="javascript">
var strM = "javascript is a good script language";
//ÔÚ´ËÎÒÏ뽫×ÖĸaÌæ»»³É×ÖĸA
alert(strM.replace(/a/,"A"));
</script>
ÕâÑù»¹ÊÇÖ»Ìæ»»Á˵ÚÒ»¸ö×Öĸa¡£
<script language="javascript">
var strM = "javascript is a good script language";
//Ôڴ˽«×ÖĸaÈ«²¿Ìæ»»³É×ÖĸA
alert(strM.replace(/a/g,"A"));
</script>
ÓÉÉÏ¿ÉÖª£¬µ±ÕýÔò±í´ïʽÓÐ"g"±ê־ʱ£¬´ú±í½«´¦ÀíÕû¸ö×Ö·û´®.
<script language="javascript">
var strM = "javascript is a good script language";
alert(strM.replace(/(javascript)\s*(is)/g,"$1 $2 fun. it $2"));
</script>
ÏÈ¿´¿´¼òµ¥Àý×Ó£º½«ËùÓе¥´ÊÊ××Öĸ»»³É´óд¡£
<script language="javascript">
var strM = "javascript is a good script language";
function change(word)
{
return word.indexOf(0).toUpperCase()+word.substring(1);
}
alert(strM.replace(/\b\w+\b/g,change));
</script>
ÓÉÉÏ¿ÉÖª£¬µ±ÕýÔò±í´ïʽÓÐ"g"±ê־ʱ£¬´ú±í½«´¦ÀíÕû¸ö×Ö·û´®£¬¼´º¯ÊýchangeµÄ±ä»»½«Ó¦ÓÃÓÚËùÓÐÆ¥ÅäµÄ¶ÔÏ󡣶ø¸Ãº¯ÊýÓÐÈý¸ö»ò¸ü¶à²ÎÊý£¬¾ßÌå¸öÊýÊÓÕýÔò±í´ïʽ¶ø¶¨¡£
ÓÐÁ˺¯ÊýÓëÕýÔò±í´ïʽµÄÅäºÏ£¬replace()´¦Àí×Ö·û´®µÄ¹¦ÄÜ¿ÕÇ°Ç¿´óÆðÀ´ÁË£¡
×îºó»¹¾Ù¸öÀý×Ó£¬½«×Ö·û´®ËùÓе¥´Êµ¹Ðò£¬ÓÃreplace()´¦ÀíÊÇÈç´Ë¼òµ¥¡£
<script language="javascript">
var strM = "javascript is a good script language";
function change(word)
{
var result = word.match(/(\w)/g);
if ( result )
{
var str = "";
for ( var i=result.length-1; i>=0; i
Ïà¹ØÎĵµ£º
±¾ÆªÈÕÖ¾ÏÈÖ÷ÒªÌÖÂÛ RegExp ¶ÔÏóµÄ²¿·Ö·½·¨£¬RegExp ¶ÔÏó¶¨ÒåÁËÁ½¸öÓÃÓÚģʽƥÅäµÄ·½·¨£¬ËüÃÇÊÇexec()
ºÍtest()
ÏÂÃæÎÒÃÇÖðÒ»½øÐн²½â
¡£ºÇ£¬ºÍÉÏƪÈÕÖ¾Ò»ÑùµÄ½éÉÜ£¬¿´À´Ì×»°£¬¹ûÈ»ÊÊÓÃÓÚ¶à¸ö³¡ºÏ¡£àÅ¡£
exec() ¼ìË÷×Ö·û´®ÖеÄÖ¸¶¨Öµ£¬·µ»ØÖµÊDZ»ÕÒµ½µÄÖµ£¬Èç¹ûûÓз¢ÏÖÆ¥Å䣬Ôò·µ»Ø null£º
Ö÷ÒªµÄRegExpģʽƥÅä·½·¨Ê ......
JavaScriptµÄÕýÔò±í´ïʽ֧³Ö²¼¶û²Ù×÷£¬³ýÁË×÷Ϊ Ñ¡ÔñµÄ“²¼¶û»ò”²Ù×÷Ö®Í⣬ÕýÔò±í´ïʽ»¹Ö§³Ö·Ö×éºÍÒýÓÃ
¡£ÆäʵѡÔñ¡¢·Ö×éºÍÒýÓõÄ×÷Ó÷ֱðΪ£ºÖ¸¶¨Ñ¡ÔñÏî¡¢¶Ô×Ó±í´ïʽ·Ö×éºÍÒýÓà ǰһ×Ó±í´ïʽµÄÌØÊâ×Ö·û¡£
ÕýÔò±í´ïʽµÄÑ¡Ôñ£º
ËùνѡÔñ¼´ÔÚ¶à¸öÌõ¼þÀïÃæÑ¡ÔñÒ»¸ö£¬±ÈÈç˵ÂôË®¹ûµÄ´óү˵ËÍÄã¸öË®¹û£¬Äã¿ÉÒÔÑ¡Ô ......
javascript²Ù×÷Cookies·½·¨¼¯½õ
function setCookie(name,value)
{
var Days = 1; //´Ë cookie ½«±»±£´æ 1 Ìì
var exp = new Date(); //new Date("December 31, 9998");
exp.setTime(exp.getTime() + Days*24*60*60*1000);
document.cookie = name + ......
function urlencode( str ) {
// http://kevin.vanzonneveld.net
// + original by: Philip Peterson
// + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// * example 1: urlencode('Kevin van Zonneveld!');
// * returns 1: 'Kevin+van+Zonneveld%21'
&nb ......