標籤:valueof 正則 min 括弧 判斷 cat click char 八進位
number
Js只有一種數字類型(包括整型,浮點型)
極大或極小的可用科學計數法來表示。(7.7123e+1)
所有js數字均為64位
Js所有的數字都儲存為浮點型
小數的最大位元是17位
0開頭的為八進位 0x開頭的為16進位
console.log(Number.MAX_VALUE); 最大值
console.log(Number.MIN_VALUE);最小值
console.log(Number.NEGATIVE_INFINITY);極大值
console.log(Number.POSITIVE_INFINITY);極小值
IsNaN 判斷是不是NaN
console.log(Number.isNaN(NaN)); true
console.log(Number.isNaN(Number.NaN)); true
console.log(Number.isNaN(0/0)); true
console.log(Number.isNaN(‘NaN‘)); false
console.log(Number.isNaN(‘‘)); false
console.log(Number.isNaN(‘123‘)); false
console.log(Number.isNaN(true)); false
console.log(Number.isNaN(undefined)); false
console.log(Number.isNaN(‘ ‘)); false
toFixed();四捨五入為指定小數位元的數字
var n=12345.6789;
console.log(n.toFixed()); 12346
console.log(n.toFixed(1)); 12345.7
console.log(n.toFixed(2)); 12345.68
console.log(n.toFixed(6)); 12345.678900
console.log(1.23e+20.toFixed(2)); 123000000000000000000.00
console.log(1.23e-20.toFixed(2)); 0.00
console.log(2.45.toFixed(1)); 2.5
toExponential(x);把對象的值轉變為指數計數法,x規定小數位元
var n=77.1234;
console.log(n.toExponential()); 7.71234e+1
console.log(n.toExponential(2)); 7.71e+1
console.log(n.toExponential(4)); 7.7123e+1
Toprecision();對象的值超出指定位元時將其轉換為指數計數法;
var n=5.1234567;
console.log(n.toPrecision()); 5.1234567
console.log(n.toPrecision(1)); 5
console.log(n.toPrecision(2)); 5.1
console.log(n.toPrecision(5)); 5.1235
console.log((1234.567).toPrecision(2)); 1.2e+3
String對象
var str=‘king‘;
console.log(typeof str); //string
var strObj=new String(‘king‘);
console.log(typeof strObj); //obj
console.log(strObj[0]); //k
console.log(strObj.length); //4
console.log(strObj.valueOf()); //king
console.log(strObj.toString()); //king
console.log("nana"[0]); //n
console.log("helloworld".length); //10
//charAt()根據下標返回指定的字元
var str=‘king‘;
console.log(str.charAt(0)); //k
console.log(str.charAt(1)); //i
console.log(str.charAt(2)); //n
console.log(str.charAt(3)); //g
console.log(str.charAt(10)); //
console.log(str.charAt(b)); //-1
//charCodeAt():返回指定字元的ASCII碼值
var str=‘abcdef‘;
console.log(str.charCodeAt(0));//a的ASCII碼值97
console.log(str.charCodeAt(100)); 空的值為 NaN
console.log(str.charCodeAt(-123)); NaN
//fromCharCode():根據指定的ASCII放回對應的字元
console.log(String.fromCharCode(97)); //a
console.log(String.fromCharCode(65,66,67));
var str=‘hello ‘;
console.log(str.concat(‘world‘));
// hello world
console.log(str.concat(‘world ‘,‘!‘));
// hello world!
//字串搜尋相關
var str=‘this is a test‘;
var str=‘this is a test‘;
console.log(str.indexOf(‘t‘)); 0
console.log(str.indexOf(‘is‘)); 2
console.log(str.indexOf(‘Is‘)); -1
console.log(str.indexOf(‘i‘)); 2
console.log(str.indexOf(‘i‘,3)); 5//此處的3指的是從第三位開始
通過indexOf()可以統計一個字元在指定字串中出現的次數
var str=‘sssssdlkfjlwk34jlksdfjlksjdlf234‘;
var count=0;
var pos=str.indexOf(‘s‘);
while(pos!=-1){
count++;
pos=str.indexOf(‘s‘,pos+1);
}
console.log(count);
lastIndexOf():最後一次出現的位置
var str=‘this is a test‘;
console.log(str.indexOf(‘is‘)); 2
console.log(str.lastIndexOf(‘is‘)); 5
比較兩個字串
console.log(‘h‘.localeCompare(‘j‘)); 104和106
大 顯示負數 後面小 顯示正數
match():找到一個或多個Regex的結果
var str=‘this is a test ofking show time‘;
varre=/IS/i; i不區分大小寫
console.log(str.match(re));
var str=‘QWERTYUIOPASDFGHJKLZXCVBNMqwertyuioasdfghjkzxcvbnm‘;
console.log(str.match(/[a-f]/ig)); g是全域匹配(搜尋)
search():根據Regex進行搜尋
var str=‘this is a test‘;
console.log(str.search(/is/)); 2
console.log(str.search(/IS/)); -1
console.log(str.search(/IS/i)); 2
var str=‘this is a test‘;
varnewStr=str.replace(/IS/ig,‘!‘); th! ! a test 把is替換成!
var str="2015-09-26";
varnewStr=str.replace(/(\d{4})-(\d{2})-(\d{2})/,"$2/$3/$1"); 09/26/2015 換位置
截取字串
var str=‘abcdef‘;
console.log(str.slice(2)); cdef
console.log(str.slice(0,2)); 左開右閉 ab 只顯示0 1 後面閉合相當於沒有
console.log(str.slice(-3)); def
console.log(str.slice(-4,-2)); cd
console.log(str.slice(0,-1)); abcde
console.log(str.substr(3)); def
console.log(str.substr(0,4)); abcd
split()將字串拆分成數組
varstr=‘2015-08-12‘;
vararr=str.split(‘-‘); ["2015", "08","12"]
console.log(arr);
varstr=‘a b c d e f‘;
vararr=str.split(‘ ‘); ["a", "b", "c", "d","e", "f"]
console.log(arr);
arr=str.split(‘‘,2); ["a", "b"]
console.log(arr);
字串大小寫相關
(大寫換小寫 小寫換大寫)
console.log("KING".toLowerCase());
console.log("KING".toLocaleLowerCase());
console.log(‘nana‘.toUpperCase());
console.log(‘nana‘.toLocaleUpperCase());
trim()
var str=‘ abc ‘;
alert("!"+str+"!"); 有空格
alert("!"+str.trim()+"!"); 沒有空格
產生錨點
varstr="this is a test";
document.body.innerHTML=str.anchor(‘contents_anchor‘); this is a test 把變數this is a test輸入進str.anchor(‘contents_anchor‘);括弧裡面
產生連結
var title=‘this is of kingshow time‘;
varurl=‘http://phpfamily.org‘;
document.write(‘ClickMe to Visit My Blog‘+title.link(url)); 把連結代替到英文字母裡 點字母跳入到連結裡
js string和number