slice() 方法提取某个字符串的一部分,并返回一个新的字符串,且不会改动原字符串
语法
str.slice(beginIndex[, endIndex])
例子
使用 slice() 创建一个新的字符串
var str1 = 'The morning is upon us.', // str1 的长度 length 是 23。
str2 = str1.slice(1, 8),
str3 = str1.slice(4, -2),
str4 = str1.slice(12),
str5 = str1.slice(30);
console.log(str2); // 输出:he morn
console.log(str3); // 输出:morning is upon u
console.log(str4); // 输出:is upon us.
console.log(str5); // 输出:""
给 slice() 传入负值索引
var str = 'The morning is upon us.';
str.slice(-3); // 返回 'us.'
str.slice(-3, -1); // 返回 'us'
str.slice(0, -1); // 返回 'The morning is upon us'