ECMAScript 6引入了String.prototype.includes
:
const string = "foo";
const substring = "oo";
console.log(string.includes(substring)); // true
不过是includes
doesn’t have Internet Explorer support.在ECMAScript 5或更早版本的环境中,使用String.prototype.indexOf
,在找不到子字符串时返回-1:
var string = "foo";
var substring = "oo";
console.log(string.indexOf(substring) !== -1); // true