startsWith()方法
startsWith()方法确定字符串是否以指定字符串的字符开头,返回true或false。
一般情况下,用于判断网址是否以http://开头。
let str = 'https://www.baidu.com';
console.log(str.startsWith('http://')) //false
console.log(str.startsWith('https://')) //true
endsWith()方法
endsWith()方法和startsWith()方法一样,确定字符串是否以指定字符串的字符结尾,返回true或false。
一般情况下,用于判断网址是否以.com结尾或者图片地址是否以.jpg结尾。
let str = 'https://www.baidu.com';
console.log(str.endsWith('.com')) //true
console.log(str.endsWith('.html')) //false
includes()方法
includes()方法判断一个字符串或数组是否包含指定的值。
let str='abcjdk'
console.log(str.includes('abc')); //true
console.log(str.includes('abcjdk')); //true
console.log(str.includes('abcd')); //false
repeat()方法
repeat(n)方法是将原字符串重复n次;
当n的值为小数会自动取整,当n为负数会报错。
let str = 'abcde'
console.log(str.repeat(3)); // abcdeabcdeabcde
console.log(str.repeat(3.7)); // abcdeabcdeabcde
console.log(str.repeat(4.1)); // abcdeabcdeabcdeabcde
console.log(str.repeat(-1)); //报错