1.startsWith()
startsWith() 方法可以检测字符串开头,有两个参数,第一个为匹配的字符串,第二个为起始位置(以0为开始),省略则为0开始:
const str = '12345abcdEF!$%';
console.log(str.startsWith('123')); //true
console.log(str.startsWith('abc', 5)); //true
区分大小写:
console.log(str.startsWith('EF',9)); //true
console.log(str.startsWith('ef',9)); //false
2.endsWith()
与 startsWith() 相对应,endsWith() 方法可以检测字符串结尾,同样有两个参数,第一个为匹配的字符串,第二个不同,为匹配最后一个字符的位置(以0为开始),省略则为最后一个字符位置:
const str = '12345abcdEF!$%';
console.log(str.endsWith('%')); //true
console.log(str.endsWith('abc', 8)); //true
它同样区分大小写:
console.log(str.endsWith('EF!$%')); //true
console.log(str.endsWith('ef!$%')); //false
3.includes()
includes() 方法用于检测原字符串是否含有一段字符串:
const str = '12345abcdEF!$%';
console.log(str.includes('abcd')); //true
同样区分大小写:
console.log(str.includes('ef')); //false
4.repeat()
repeat() 方法能够返回原字符串复制参数次数之后形成的新字符串:
console.log('古德'.repeat(2)); //古德古德
console.log('6'.repeat(10)); //6666666666