ES6 判断字符串以什么开头或者什么结尾

startsWith()方法确定字符串是否以指定字符串的字符开头,返回true或false。
endsWith()方法和startsWith()方法的语法都是一样的,不过endsWith()方法是从字符串的末尾开始查找。

let str = "https://C:/Users/2/1.png";
console.log(str.startsWith("https://"))// true;
console.log(str.startsWith("http://"))// false;
console.log(str.endsWith(".jpg"))// false;
console.log(str.endsWith(".png"))// true;

猜你喜欢

转载自blog.csdn.net/m0_47402657/article/details/106505011