1 var test = 'hello world';
2
3 alert(test.slice(4,7)); //o w
4 alert(test.substring(4,7)); //o w
5 alert(test.substr(4,7)); //o world
alert(test.substring(7,4)); //o w
1 var test = 'hello world';
2
3 alert(test.slice(-3)); //rld
4 alert(test.substring(-3)); //hello world
5 alert(test.substr(-3)); //rld
6 alert(test.slice(3,-4)); //lo w
7 alert(test.substring(3,-4)); //hel
8 alert(test.substr(3,-4)); //空字符串
1 注意:IE對substr接收負值的處理有錯,它會返回原始字符串。