.includes()在Internet Explorer中不起作用

.includes()在Internet Explorer中不起作用

问题描述:

此代码在Internet Explorer中不起作用.还有其他选择吗?

This code does not work in internet explorer. Any alternative?

"abcde".includes("cd")

在撰写本文时,

String.prototype.includes在Internet Explorer(或Opera)中不受支持.

String.prototype.includes is, as you write, not supported in Internet Explorer (or Opera).

相反,您可以使用String.prototype.indexOf. #indexOf返回子字符串的第一个字符的索引(如果它在字符串中),否则返回-1. (非常类似于Array)

Instead you can use String.prototype.indexOf. #indexOf returns the index of the first character of the substring if it is in the string, otherwise it returns -1. (Much like the Array equivalent)

var myString = 'this is my string';
myString.indexOf('string');
// -> 11

myString.indexOf('hello');
// -> -1

MDN使用indexOfincludes提供了一个polyfill: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/includes#Polyfill

MDN has a polyfill for includes using indexOf: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/includes#Polyfill

版本28 开始,Opera支持includes.

Opera supports includes as of version 28.

Edge的当前版本支持该方法. (截至2019年)

EDIT 2: Current versions of Edge supports the method. (as of 2019)