.includes ()가 Internet Explorer에서 작동하지 않음


105

이 코드는 Internet Explorer에서 작동하지 않습니다. 대안이 있습니까?

"abcde".includes("cd")

33
2 년이 지난 후에도 IE는 여전히이를 지원하지 않습니다.
nu everest

4
IE가 나아지기를 기다리는 것은 마치 ... IE가 나아지기를 기다리는 것과 같습니다.
Rob_M

1
@nueverest 당신은 3 년을 의미합니까? : D
Josh

2
누군가 모두에게 호의를 베풀고 IE의 저장소를 삭제합니다. 그냥 끝내세요.
zero_cool

추가 2 년
-IE

답변:


131

String.prototype.includes 작성시 Internet Explorer (또는 Opera)에서 지원되지 않습니다.

대신 String.prototype.indexOf. #indexOf문자열에 있으면 하위 문자열의 첫 번째 문자의 인덱스를 반환하고 그렇지 않으면 반환합니다.-1 . (배열과 매우 유사 함)

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

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

MDN에는 다음을 includes사용 하기위한 polyfill이 있습니다 indexOf. https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/includes#Polyfill

편집 : 오페라 지원 includes 현재의 버전 (28) .

편집 2 : Edge의 현재 버전은 방법을 지원합니다. (2019 년 기준)


IE에서 지원하지 않는 유일한 함수는 include ()입니까? 아니면 IE에서 지원하지 않는 다른 타이프 스크립트 나 JavaScript 함수가 있습니까?
Abdullah Feroz

10
우리가 필요하면 Boolean, 우리는 할 수(myString.indexOf('string') > -1) // to get a boolean true or false
아카 쉬

32

아니면 그냥 자바 스크립트 파일에 넣고 좋은 하루 보내세요 :)

String.prototype.includes = function (str) {
  var returnValue = false;

  if (this.indexOf(str) !== -1) {
    returnValue = true;
  }

  return returnValue;
}

이 polyfill을 사용하는 경우을 사용하여 문자열을 반복하지 마십시오 . 이렇게 정의되면 for...in반복 String.prototype.includes됩니다.
Patrick Roberts

10
더 짧은 버전 :return this.indexOf(str) !== -1;
Andrew

1
배열의 경우 : Array.prototype.includes = function (elt) {return this.indexOf (elt)! == -1; }
LePatay 19

9

includes ()는 대부분의 브라우저에서 지원되지 않습니다. 귀하의 옵션은

-polyfill from MDN https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/includes

또는 사용

-indexof ()

var str = "abcde";
var n = str.indexOf("cd");

n = 2를줍니다.

이것은 널리 지원됩니다.


MDN의 polyfill을 사용하는 경우 문자열을 for...in! , 그렇게 String.prototype.includes정의 하면 반복됩니다 .
Patrick Roberts

6

문제:

Internet Explorer에서 아래 (솔루션없이) 실행 하고 결과를 확인하십시오.

console.log("abcde".includes("cd"));

해결책:

이제 솔루션 아래에서 실행하고 결과를 확인하십시오.

if (!String.prototype.includes) {//To check browser supports or not
  String.prototype.includes = function (str) {//If not supported, then define the method
    return this.indexOf(str) !== -1;
  }
}
console.log("abcde".includes("cd"));


4

이것은 더 좋고 더 짧을 수 있습니다.

function stringIncludes(a, b) {
    return a.indexOf(b) >= 0;
}

같이 IndexOf는 IE에서 지원하지 않는
Some_Dude

1
IE11에서 완벽하게 작동합니다. IE10에는 없을 수도 있지만 요즘에는 여전히 그 버전을 사용하는 사람이 거의 없습니다.
Andrew

3

Angular 5에서 작업 할 때도 같은 문제가있었습니다. 직접 polyfill을 작성하지 않고 직접 작동하게하려면 polyfills.ts 파일에 다음 줄을 추가하면됩니다.

import "core-js/es7/array"

또한 tsconfig.jsonlib 섹션이 관련 될 수 있습니다.

"lib": [
  "es2017",
  "dom"
],

내 친구는 완전한 생명의 은인입니다!
CodeMan03

2

반응 :

import 'react-app-polyfill/ie11';
import 'core-js/es5';
import 'core-js/es6';
import 'core-js/es7';

에 대한 문제 해결-includes (), find () 등 ..


1

Array.prototype.include()in javascript 를 계속 사용 하려면이 스크립트를 사용할 수 있습니다. github-script-ie-include IE를 감지하면 include ()를 match () 함수로 자동 변환합니다.

다른 옵션은 항상string.match(Regex(expression))


1

나를 위해 작동합니다.

function stringIncludes(a, b) {
      return a.indexOf(b) !== -1;
}

당사 사이트를 사용함과 동시에 당사의 쿠키 정책개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.