如何在Javascript中检查字符串中是否包含子字符串?
0 1239
0
该提问暂无详细描述
收藏
2021-02-12 13:37 更新 小眼的铁板烧 •  3502
共 1 个回答
高赞 时间
0

ECMAScript6引入了 String.prototype.includes

const string = "foo";
const substring = "oo";
console.log(string.includes(substring));

但是includes 不支持Internet Explorer。在ECMAScript 5或更以前的环境下,可以使用String.prototype.indexOf,当找不到子字符串时,返回-1:

var string = "foo";
var substring = "oo";
console.log(string.indexOf(substring) !== -1);

转载自:https://stackoverflow.com/questions/1789945/how-to-check-whether-a-string-contains-a-substring-in-javascript

收藏
2021-02-12 14:22 更新 空心人 •  3330