全部 JavaScript 數據類型都有 valueOf() 和 toString() 方法。
length
:返回字符串的長度。正則表達式
全部字符串方法都會返回新字符串。它們不會修改原始字符串。
indexof() 數組
返回字符串中指定文本首次出現的位置
lastIndexof() 函數
返回字符串中指定文本最後一次出現的位置
若是未找到文本,indexOf()
和 lastIndexOf()
均返回 -1。兩種方法都接受第二個參數(非負值)做爲檢索的開始位置。this
search() 編碼
返回字符串中指定文本首次出現的位置
search()
沒法設置第二個參數做爲檢索的開始位置。indexof()
沒法設置更強大的正則表達式。spa
提取[start,end)部分
slice(start,end)
substring(start,end)
substr(start,length)prototype
區別:substring()
沒法接受負的索引。substr()
第二個參數是提取字符串的長度。code
replace()blog
默認replace() 只替換首個匹配。默認 replace() 對大小寫敏感。
let str = "tom is toms"; console.log(str.replace("tom", "bob"));
toUpperCase() 把字符串轉換爲大寫:
toLowerCase() 把字符串轉換爲小寫:索引
鏈接兩個或多個字符串:
let str1 = "hello"; let str2 = "world"; console.log(str1.concat(" ", str2)); //hello world
刪除字符串兩端的空白符。
charAt(position)
返回字符串中指定下標(位置)的字符:
charCodeAt(position)
返回字符串中指定索引的字符 unicode 編碼
var str = "HELLO WORLD"; str.charAt(0); // 返回 H str.charCodeAt(0); // 返回 72
var str = "HELLO WORLD"; str[0]; // 返回 H
屬性訪問有問題,不推薦使用:
若是找不到字符,[ ] 返回 undefined
,而 charAt()
返回空字符串。
它是隻讀的。str\[0] = "A"
不會產生錯誤(但也不會工做!)
若是但願按照數組的方式處理字符串,能夠先把它轉換爲數組。
把字符串轉換爲數組
若是省略分隔符,被返回的數組將包含 index [0] 中的整個字符串。
若是分隔符是 "",被返回的數組將是間隔單個字符的數組:
let txt = "a,b,c,d"; console.log(txt.split(",")); //[ 'a', 'b', 'c', 'd' ] console.log(txt.split(" ")); //[ 'a,b,c,d' ] console.log(txt.split("|")); //[ 'a,b,c,d' ] console.log(txt.split("")); //['a', ',', 'b',',', 'c', ',','d']
let x = 123e5; // 12300000 let y = 123e-5; // 0.00123
Infinity
(或 -Infinity
)除以 0(零)也會生成 Infinity:
屬性 | 描述 |
---|---|
MAX_VALUE | 返回 JavaScript 中可能的最大數。 |
MIN_VALUE | 返回 JavaScript 中可能的最小數。 |
NaN | 非數值 |
NEGATIVE_INFINITY | 表示負的無窮大(溢出返回)。 |
POSITIVE_INFINITY | 表示無窮大(溢出返回)。 |
toString()
以字符串返回數值。返回值類型爲string
let x = 123; console.log(x.toString()); //123 console.log("123".toString()); //123 console.log((120 + 3)); //123
把數輸出爲十六進制、八進制或二進制。
var myNumber = 128; myNumber.toString(16); // 返回 80 myNumber.toString(8); // 返回 200 myNumber.toString(2); // 返回 10000000
toExponential()
返回字符串值,它包含已被四捨五入並使用指數計數法的數字。參數定義小數點後的字符數:
let x = 9.656; x.toExponential(2); // 返回 9.66e+0 x.toExponential(4); // 返回 9.6560e+0 x.toExponential(6); // 返回 9.656000e+0
toFixed()
返回字符串值,它包含了指定位數小數的數字。
var x = 9.656; x.toFixed(0); // 返回 10 x.toFixed(2); // 返回 9.66 x.toFixed(4); // 返回 9.6560 x.toFixed(6); // 返回 9.656000
toPrecision()
返回字符串值,它包含了指定長度的數字。四捨五入
var x = 9.656; x.toPrecision(); // 返回 9.656 x.toPrecision(2); // 返回 9.7 x.toPrecision(4); // 返回 9.656 x.toPrecision(6); // 返回 9.65600
valueOf()
以數值返回數值
var x = 123; x.valueOf(); // 從變量 x 返回 123 (123).valueOf(); // 從文本 123 返回 123 (100 + 23).valueOf(); // 從表達式 100 + 23 返回 123
這些方法並不是數字方法,而是全局 JavaScript 方法。
Number() 方法
Number() 可用於把 JavaScript 變量轉換爲數值,若是沒法轉換數字,則返回 NaN。
parseInt() 方法
解析一段字符串並返回數值。容許空格。只返回首個數字。
parseInt("10"); // 返回 10 parseInt("10.33"); // 返回 10 parseInt("10 20 30"); // 返回 10 parseInt("10 years"); // 返回 10 parseInt("years 10"); // 返回 NaN
parseFloat() 方法
解析一段字符串並返回數值。容許空格。只返回首個數字。
parseFloat("10"); // 返回 10 parseFloat("10.33"); // 返回 10.33 parseFloat("10 20 30"); // 返回 10 parseFloat("10 years"); // 返回 10 parseFloat("years 10"); // 返回 NaN
/正則表達式主體/修飾符(可選)
字符串方法
用於檢索字符串中指定的子字符串,或檢索與正則表達式相匹配的子字符串,並返回子串的起始位置。
let str = "This is a code. Write By Pillar"; let n = str.search(/code/i); console.log(n);//10
用於在字符串中用一些字符替換另外一些字符,或替換一個與正則表達式匹配的子串。
let str = "This is a code. Write By Pillar"; let n = str.replace("code", "word"); console.log(n);//This is a word. Write By Pillar
RegExp 方法
用於檢測一個字符串是否匹配某個模式,若是字符串中含有匹配的文本,則返回 true,不然返回 false。
let str = "This is a code. Write By Pillar"; let reg = /code/; console.log(reg.test(str));//true
該函數返回一個數組,其中存放匹配的結果。若是未找到匹配,則返回值爲 null。
let str = "This is a code. Write By Pillar"; let reg = /code/; console.log(reg.exec(str)); /*[ 'code', index: 10, input: 'This is a code. Write By Pillar', groups: undefined ] */
修飾符
修飾符 | 描述 |
---|---|
i | 執行對大小寫不敏感的匹配。 |
g | 執行全局匹配(查找全部匹配而非在找到第一個匹配後中止)。 |
m | 執行多行匹配。 |
模式
表達式 | 描述 |
---|---|
[abc] | 查找方括號之間的任何字符。 |
[0-9] | 查找任何從 0 至 9 的數字。 |
(x l y) | 查找任何以 l 分隔的選項。 |
元字符 | 描述 | 元字符 | 描述 |
---|---|---|---|
d | 查找數字 | D | 非數字 |
s | 查找空白字符 | S | 非空白字符 |
w | 數字字母下劃線 | W | 非數字字母下劃線 |
b | 匹配單詞邊界 | . | 任意字符 |
l | 或 | [^] | 取反 |
^ | 正則表達式開始匹配的位置 | $ | 結束的位置 |
uxxxx | 查找以十六進制數 xxxx 規定的 Unicode 字符。 |
量詞 | 描述 |
---|---|
n+ | 匹配任何包含一個或多個 n 的字符串。 |
n* | 匹配任何包含零個或多個 n 的字符串。 |
n? | 匹配任何包含零個或一個 n 的字符串。 |
X{n} | 出現n次X |
X{n,} | 至少出現n次X |
X{n,m} | 至少出現n次,最多出現m次X |
郵箱的正則表達式:
^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$
手寫字符串trim方法
String.prototype.trim = function (){ return this.replace(/^\s+/,'').replace(/\s+$/,'') }