javascript对象小结 时间:2020-05-12 人气:0 距离某天还有天 javascript对象 <script language="javascript" type="text/javascript"> var regExp=/\s\*/g; /*---------------string对象------------------*/ var str="this *is *test *string"; var sTxt=str.charAt(3); var aTxt=str.charCodeAt(3); var btxt=str.indexOf("s"); var cTxt=str.slice(0,4); var dTxt=str.split(" "); var subTxt=str.substr(0,4); var regTxt=str.match(regExp); var regSearchTxt=str.search(regExp); document.writeln("原始字符串:"+str+""); document.writeln("charAt(3),返回索引字符:"+sTxt+""); document.writeln("charCodeAt(3),返回unicode字符代码:"+aTxt+""); document.writeln("indexOf,返回指定字符索引"+btxt+""); document.writeln("slice(0,4),返回0,4之间的字符:"+cTxt+""); document.writeln("split(' '),以空格截取字符成数组:"+dTxt+""); document.writeln("substr(0,4),截取两个索引之间的字符:"+subTxt+""); document.writeln("match,返回匹配的字符串:"+regTxt+""); document.writeln("search,返回匹配的字符串的便宜位置:"+regSearchTxt+""); /*-----------------正则表达式---------------*/ //test var result_tst=regExp.test(str); document.writeln("regExp.test(str),返回是否匹配:"+result_tst+""); //exec var result_exec=regExp.exec(str); document.writeln("返回匹配的内容:"+result_exec+""); var result=str.replace(regExp,"_"); document.writeln("返回替换的内容:"+result+""); //匹配规则 var reg2=/:\s*\w*\s*[0-9]+\s[0-9]{4}/; var reg1=/:\D*\s*\d+\s\d+/; var reg3=/w*@w+.w/; var str2="this is a date: march 12 2010"; var eTxt=str2.match(reg2); document.writeln(eTxt+""); //*----------------Date对象--------------------*/ var now=new Date(); var hours=now.getHours()+7*24; now.setHours(hours); document.write("一个星期后是:"+now+""); document.write("现在是"+now.getFullYear() +"年"+(now.getMonth()+1)+"月"+now.getDate() +"日"+now.getHours()+"时"+now.getMinutes() +"分"+now.getSeconds()+"秒"+""); //计时器 function countTime(){ var targetTime = new Date(2010,6,24,12,0,0,0); var nowTime = new Date(); //距离目标时间 var distanTime=targetTime.getTime()-nowTime.getTime(); days = Math.floor( distanTime / 1000 / 60 / 60 / 24); hours = Math.floor(distanTime/ 1000 / 60 / 60 - (24 * days)); minutes = Math.floor( distanTime/1000/60-(24*60*days)-(60*hours)); seconds =distanTime/1000-(24*60*60*days)-(60*60*hours)-(60*minutes); secondsRound = Math.round(seconds); document.getElementById("days").innerHTML=""+days+""; document.getElementById("time").innerHTML=hours+":"+minutes+":"+secondsRound; } setInterval("countTime()",250); //数组 var firstArr=new Array("one","two","three"); var secondArr=["one","two","three"]; var remove=firstArr.slice(2,3); var splicArr=secondArr.splice(1,1); document.writeln(remove+""); document.writeln(splicArr+""); //队列 var fifoArr=new Array(); fifoArr.push("apple"); fifoArr.push("banana"); var length=fifoArr.push("cherry"); document.writeln("数组的长度是:"+length+" 数组内容是:"+fifoArr+""); fifoArr.shift(); document.writeln("使用shift命令删除第一个元素:"+fifoArr+""); fifoArr.unshift("addFruite"); document.writeln("使用unshift命令增加第一个元素:"+fifoArr+""); fifoArr.pop(); document.writeln("使用pop命令最后一个元素:"+fifoArr+""); </script> 距离某天还有天 [Ctrl+A 全选 注:如需引入外部Js需刷新才能执行] 加载全部内容