字串是日常開發中打交道最多的資料型別之一,今天我們就來系統梳理 String 物件那些既常用又容易混淆的方法。
/* match() 可在字串中檢索指定值,或找到一個或多個正則表達式的匹配。
與 indexOf() 不同,它返回的是匹配到的內容本身,而非位置。
語法:stringObject.match(searchvalue) / stringObject.match(regexp) */
var text = "cat, bat, sat, fat";
var pattern = /.at/;
var matches = text.match(pattern);
alert(matches.index); // 0
alert(matches[0]); // "cat"
alert(pattern.lastIndex); // 0
/* search() 用於檢索字串中第一個與 regexp 匹配的子串起始位置,
找不到則返回 -1;追加標志 i 可忽略大小寫。 */
var pos = text.search(/at/);
alert(pos); // 1
/* replace() 用於替換匹配內容,返回替換後的新字串。
若第一個參數是字串,僅替換首次出現;若為帶 g 的正則,則替換全部。 */
var result = text.replace("at", "ond");
alert(result); // "cond, bat, sat, fat"
result = text.replace(/at/g, "ond");
alert(result); // "cond, bond, sond, fond"
result = text.replace(/(.at)/g, "word ($1)");
alert(result); // word (cat), word (bat), word (sat), word (fat)
// 結合回呼函數實現 HTML 轉義
function htmlEscape(text){
return text.replace(/[<>"&]/g, function(match, pos, originalText){
switch(match){
case "<": return "<";
case ">": return ">";
case "&": return "&";
case "\"": return """;
}
});
}
alert(htmlEscape("<p class=\"greeting\">Hello world!</p>"));
/* split() 把字串分割成陣列。
若 separator 含子表達式,返回的陣列會包含這些子表達式的匹配結果。 */
var colorText = "red,blue,green,yellow";
var colors1 = colorText.split(","); // ["red","blue","green","yellow"]
var colors2 = colorText.split(",", 2); // ["red","blue"]
var colors3 = colorText.split(/[^\,]+/); // ["",",",",",",",""]var stringValue = "hello world"; alert(stringValue.toLocaleUpperCase()); // "HELLO WORLD" alert(stringValue.toUpperCase()); // "HELLO WORLD" alert(stringValue.toLocaleLowerCase()); // "hello world" alert(stringValue.toLowerCase()); // "hello world"
var stringObject = new String("hello world");
var stringValue = "hello world";
alert(typeof stringObject); // "object"
alert(typeof stringValue); // "string"
alert(stringObject instanceof String); // true
alert(stringValue instanceof String); // false/* fromCharCode() 接受一個或多個 Unicode 值,返回對應組成的字串。 */ alert(String.fromCharCode(104, 101, 108, 108, 111)); // "hello"
/* 按本地特定順序比較兩個字串,返回一個數字:
小於 0 表示前者在前,大於 0 表示在後,等於 0 表示相等。 */
var stringValue = "yellow";
alert(stringValue.localeCompare("brick")); // 1
alert(stringValue.localeCompare("yellow")); // 0
alert(stringValue.localeCompare("zoo")); // -1
function determineOrder(value) {
var result = stringValue.localeCompare(value);
if (result < 0){
alert("The string 'yellow' comes before the string '" + value + "'.");
} else if (result > 0) {
alert("The string 'yellow' comes after the string '" + value + "'.");
} else {
alert("The string 'yellow' is equal to the string '" + value + "'.");
}
}/* indexOf() 返回指定值首次出現的位置,可傳 fromindex 指定起點。
lastIndexOf() 則從後向前搜索,返回最後一次出現的位置。 */
var stringValue = "hello world";
alert(stringValue.indexOf("o")); // 4
alert(stringValue.lastIndexOf("o")); // 7
alert(stringValue.indexOf("o", 6)); // 7
alert(stringValue.lastIndexOf("o", 6)); // 4/* slice()、substring()、substr() 都能截取子字串,但細節不同: - slice(start, end):支持負數索引,取 start 到 end-1。 - substring(start, stop):不支持負數,若 start > stop 會自動交換。 - substr(start, length):第二個參數是「長度」而非結束位置,支持負數 start。 */ var stringValue = "hello world"; alert(stringValue.slice(3)); // "lo world" alert(stringValue.substring(3)); // "lo world" alert(stringValue.substr(3)); // "lo world" alert(stringValue.slice(3, 7)); // "lo w" alert(stringValue.substring(3, 7)); // "lo w" alert(stringValue.substr(3, 7)); // "lo worl" alert(stringValue.slice(-3)); // "rld" alert(stringValue.substring(-3)); // "hello world" alert(stringValue.substr(-3)); // "rld" alert(stringValue.slice(3, -4)); // "lo w" alert(stringValue.substring(3, -4)); // "hel" alert(stringValue.substr(3, -4)); // "" (空字串)
以上就是今天整理的 String 物件常用方法,後續還會持續更新,敬請關注。