ECMAscript新特性 - 字符串的扩展方法

108 阅读1分钟

ECMAscript 2015 当中还为字符串对象提供了一系列扩展方法,比如 includes()startsWith()endsWith() 。它们是一组方法可以用来去更方便的判断字符串当中是否包含指定的内容,相比于之前使用 indexOf() 或者是正则表达式来判断这样一组方法会让字符串查找便捷很多。

const msg = 'Error: foo is not defined.';
console.log(msg.startWith('Error')) // true
console.log(msg.endWith('.')) // true
console.log(msg.includes('foo')) // true