<html> <head> <mce:script type="text/javascript"><!-- window.onload = init; function init(){ //testReplace(); testReplaceRegExp(); //testReplaceRegExpFunction(); } function testReplace(){ var str = "this is a red color red"; //替换的是搜册出现的字符串 var temp = str.replace("red","blue"); //值得注意的是str的内容始终没有改变 alert(str +"==>"+temp) } function testReplaceRegExp(){ var str = "where are you my dog and dog and Dog"; var temp = str.replace(/dog/gi,"fish"); alert(str +"==>"+temp) } function testReplaceRegExpFunction(){ var str = "The sky is blue"; //sMatch 是要匹配的字符串 var temp = str.replace(/blue/,function(sMatch){return "red";}); alert(temp); } // --></mce:script> </head> <body> </body> </html>