標籤:locale 正則 timezone style turn 完整 測試 1.5 IV
JavaScript 基礎學習(二)instanceof方法:
var s = "hello"; var i = 8; //typeof 只能判斷基礎資料型別 (Elementary Data Type) alert(typeof(s)); alert(typeof (i)); //對於引用資料類型,用instanceof var s2=new String("hello2") alert(typeof(s2)); alert(s2 instanceof String);//true var n = new Number(2); alert(typeof(n)); alert(n instanceof Number);//true
String對象
自動建立字串對象:
var str1="hello world";
手工建立字串對象:
var str1= new String("hello word");
String屬性與方法:
<script> var s = "hELlo"; // String對象的屬性 length alert(s.length); // 遍曆字串 for (var i in s){console.log(s[i])} //重複的會放一起 // 編排方法 document.write(s.italics());//返回斜體字串 document.write(s.bold());//返回粗體表示字串 document.write(s.anchor("alex"));//返回錨定義字串<a>x</a> // 大小寫轉換 console.log(s.toUpperCase());//HELLO console.log(s.toLowerCase());//hello // 擷取指定字元 console.log(s.charAt(3)) //l console.log(s.charCodeAt(3))//108 asc碼值 // 查詢字串 console.log(s.search("l")) ;//返回的第一個匹配結果的索引值 console.log(s.match("l")[0]); //返回數組,裡面是所有匹配結果 console.log(s.match("l")[1]); // replace concat split console.log(s.replace("E","e")); console.log(s.split("E")); console.log(s.concat("wordl")); // 截取字串 console.log(s.substr(1,2));//El 按個數取 console.log(s.substring(1,2));//E 按索引取 console.log(s.slice(1,-1)) ; //ELl console.log(s.indexOf("l"));//從前往後取 console.log(s.lastIndexOf("l")); //從後面取索引值 </script>
分割字串
var str1="一,二,三,四,五,六,日"; var strArray=str1.split(",");alert(strArray[1]); //結果為"二"
連接字串
var str1="abcd"; var str2=str1.concat("efgh");alert(str2); //結果為"abcdefgh"
Array對象
建立方式1:var a=[1,2,3];建立方式2:new Array(); // 建立數組時允許指定元素個數也可以不指定元素個數。new Array(size);//if 1個參數且為數字,即代表size,not content初始化數組對象:var cnweek=new Array(7);
Array對象的屬性
擷取數組元素的個數:length
Array對象的方法
串連數組-join方法
var arr1=[1, 2, 3, 4, 5, 6, 7];var str1=arr1.join("-");alert(str1);//結果為"1-2-3-4-5-6-7" 進出棧操作
var arr1=[1,2,3];arr1.push(4,5);alert(arr1);//結果為"1,2,3,4,5"arr1.push([6,7]);alert(arr1)//結果為"1,2,3,4,5,6,7"arr1.pop();alert(arr1);//結果為"1,2,3,4,5"
Date對象
//方法1:不指定參數var nowd1=new Date();alert(nowd1.toLocaleString( ));//方法2:參數為日期文字var nowd2=new Date("2004/3/20 11:12");alert(nowd2.toLocaleString( ));var nowd3=new Date("04/03/20 11:12");alert(nowd3.toLocaleString( ));//方法3:參數為毫秒數var nowd3=new Date(5000);alert(nowd3.toLocaleString( ));alert(nowd3.toUTCString());//方法4:參數為年月日小時分鐘秒毫秒var nowd4=new Date(2004,2,20,11,12,0,300);alert(nowd4.toLocaleString( ));//毫秒並不直接顯示
Date對象的方法—擷取日期和時間
擷取日期和時間getDate() 擷取日getDay () 擷取星期getMonth () 擷取月(0-11)getFullYear () 擷取完整年份getYear () 擷取年getHours () 擷取小時getMinutes () 擷取分鐘getSeconds () 擷取秒getMilliseconds () 擷取毫秒getTime () 返回累計毫秒數(從1970/1/1午夜)
設定日期和時間
//設定日期和時間//setDate(day_of_month) 設定日//setMonth (month) 設定月//setFullYear (year) 設定年//setHours (hour) 設定小時//setMinutes (minute) 設定分鐘//setSeconds (second) 設定秒//setMillliseconds (ms) 設定毫秒(0-999)//setTime (allms) 設定累計毫秒(從1970/1/1午夜)
Date對象的方法—日期和時間的轉換
日期和時間的轉換:getTimezoneOffset():8個時區×15度×4分/度=480;返回本地時間與GMT的時間差,以分鐘為單位toUTCString()返回國際標準時間字串toLocalString()返回本地格式時間字串Date.parse(x)返回累計毫秒數(從1970/1/1午夜到本地時間)Date.UTC(x)返回累計毫秒數(從1970/1/1午夜到國際時間)
練習
//自訂日期時間的顯示方式: function getCurrentDate() { var date=new Date(); var year= date.getFullYear(); var month = date.getMonth(); var day = date.getDate(); var hour = date.getHours(); var min = date.getMinutes(); var sec = date.getSeconds(); var week = date.getDay(); return year + "年" + changeNum(month) + "月" + day + "日" + hour + ":" + min + ":" + sec + " " + parse_week(week); } function changeNum(num) { if (num < 10){ return "0"+num; }else {return num}; } function parse_week(week){ var week_arr = ["星期日","星期一","星期二","星期三","星期四","星期五","星期六"]; return week_arr[week]; } alert(getCurrentDate())
RegExp對象
//----------------------------建立方式1 /* var reg1 = new RegExp("^[a-zA-Z][a-zA-Z0-9_]{5,11}$","g"); // //驗證字串 var str = "bc123"; alert(reg1.test(str));// true //----------------------------建立方式2 /填寫Regex/匹配模式; var reg2 = /^[a-zA-Z][a-zA-Z0-9_]{5,11}$/g; alert(reg2.test(str));// true */ //-------------------------------正則對象的方法------------------- //test方法 ==> 測試一個字串是否複合 正則規則. 傳回值是true 和false. //-------------------------String 中與正則結合的4個方法------------------. // macth search split replace var str = "hello world"; //alert(str.match(/o/g)); //尋找字串中 複合正則的 內容. //alert(str.search(/h/g));// 0 尋找字串中符合Regex的內容位置 //alert(str.split(/o/g)); // 按照Regex對字串進行切割. 返回數組; alert(str.replace(/o/g, "s")); // hells wsrld 對字串按照正則進行替換.
var reg1= new RegExp("\d+","g"); var str="abc345segd"; alert(reg1.test(str));alert(str.match(/\d+/g)); alert(str.search(/\d+/g));alert(str.split(/\d+/g));alert(str.replace(/\d+/g,"aaa"))Math對象
alert(Math.random()); // 獲得隨機數 0~1 不包括1.alert(Math.round(1.5)); // 四捨五入//練習:擷取1-100的隨機整數,包括1和100var num=Math.random();num=num*100;num=Math.round(num);alert(num)
abs(x) 返回數的絕對值。exp(x) 返回 e 的指數。floor(x)對數進行下舍入。log(x) 返回數的自然對數(底為e)。max(x,y) 返回 x 和 y 中的最高值。min(x,y) 返回 x 和 y 中的最低值。pow(x,y) 返回 x 的 y 次冪。random() 返回 0 ~ 1 之間的隨機數。round(x) 把數四捨五入為最接近的整數。sin(x) 返回數的正弦。sqrt(x) 返回數的平方根。tan(x) 返回角的正切。
JavaScript 基礎學習(二)