HTML
stringObject.indexOf(searchvalue,fromIndex) 字串尋找「由左至右尋找」將從頭到尾地檢索字串 stringObject, 是否包含有 searchvalue。 檢索開始的位置 fromIndex (沒有指定 fromIndex 時)。 如果找到一個 searchvalue ,轉回 searchvalue 所得位置。 "0123456789ABCDEF".indexOf("456") = "0123456789ABCDEF".indexOf("456",2) = "0123456789ABCDEF".indexOf("456",8) = "0123456789ABCDEF".indexOf("AB") = "0123456789ABCDEF".indexOf("939") =
stringObject.lastIndexOf(searchvalue,fromIndex) 字串反向尋找「由右至左尋找」(反向)將從尾到頭地檢索字串, stringObject 看它是否含有子串 searchvalue。 開始檢索的位置(左)在字串的 fromIndex 處或沒有指定則為字串的結尾。 var TxT = "0123456789ABCDEF" TxT.lastIndexOf("456",15) = TxT.lastIndexOf("456",2) = TxT.lastIndexOf("456",8) = TxT.lastIndexOf("AB") = TxT.lastIndexOf("939") =
stringObject.search(regexp) 傳回尋找到的「字串位置」regexp 為待尋找的字串, 可加 /g 代表總體尋找,可加 /i 代表不分大小寫。 "As As as As".search("as") = "As As as As".search(/as/) = "As As as As".search("As") = "As As as As".search(/As/g) = "As As as As".search(/As/gi) =
string.startsWith(searchvalue, start) 方法用於 檢測 searchvalue 是否是指定的子串開始。 searchvalue : 要查找的字符串 start : 查找的開始位置,默認為 0。 "hi Hello World".startsWith("hi") = "hi Hello World".startsWith("Hello") = "hi Hello World".startsWith("World") = "hi Hello World".startsWith("World",9) =
stringObject.endsWith(searchvalue, start) 方法用於 檢測 searchvalue 是否是指定的子串結尾。 searchvalue : 要查找的字符串 start : 查找的開始位置,默認為 0。 "hi Hello World".endsWith("hi") = "hi Hello World".endsWith("Hello") = "hi Hello World".endsWith("World") = "hi Hello World".endsWith("Hello",11) =
RegExpObject.test(string) RegExpObject : 字符串是否全在。 string : 要檢測的字符串。 /hi/.test('hi Hello World') = /Hello/.test('hi Hello World') = /World/.test('hi Hello World') = /love/.test('hi Hello World') = /r/.test('hi Hello World') = /R/.test('hi Hello World') =
stringObject.substr(start, length) start : 是一個數字,位置從 0 開始, 如果 start 是一個負數,從尾倒數。 length : 總共要取出幾個字元, 如果 length 大個字串 取到字串結尾。 var TxT = "0123456789ABCDEF" TxT.length = 16 TxT.substr( 0) = TxT.substr( 4) = TxT.substr(-4) = TxT.substr(4,3) = TxT.substr(4,30) = TxT.substr(-4,4) =
stringObject.substring(start, index) start : 必需。一個非負的整數,開始位置。 index : 可選。一個非負的整數,結束位置,如果省略該參數, 那麼返回的子串會一直到字符串對象的結尾。 "0123456789ABCDEF".length = 16 "0123456789ABCDEF".substring( 0) = "0123456789ABCDEF".substring( 4) = "0123456789ABCDEF".substring(-4) = "0123456789ABCDEF".substring(4,6) = "0123456789ABCDEF".substring(4,30) = "0123456789ABCDEF".substring(-4,4) =
stringObject.slice(start, end) start : 要取得部份字串的起始。如果是負數, 則該參數規定的是從字串的尾部開始算起的位置。 end : 接著要取得部份字串的結尾。若未指定此參數, 則要提取的子串包括 start 到原字串結尾的字串。 var TxT = "0123456789ABCDEF" TxT.length = 16 TxT.slice( 0) = TxT.slice( 4) = TxT.slice(-4) = TxT.slice(4,6) = TxT.slice(4,30) = TxT.slice(-6,-3) = TxT.slice(6,-3) =