通常我希望有一种String.contains()方法,但是似乎没有。
String.contains()
有什么合理的方法来检查?
ECMAScript 6引入了String.prototype.includes:
String.prototype.includes
const string = "foo"; const substring = "oo"; console.log(string.includes(substring));
includes 但是不支持Internet Explorer。在ECMAScript 5或更旧的环境中,使用String.prototype.indexOf,当找不到子字符串时,它将返回-1:
includes
Internet Explorer
String.prototype.indexOf
var string = "foo"; var substring = "oo"; console.log(string.indexOf(substring) !== -1);