답변:
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 년 기준)
Boolean, 우리는 할 수(myString.indexOf('string') > -1) // to get a boolean true or false
아니면 그냥 자바 스크립트 파일에 넣고 좋은 하루 보내세요 :)
String.prototype.includes = function (str) {
var returnValue = false;
if (this.indexOf(str) !== -1) {
returnValue = true;
}
return returnValue;
}
for...in반복 String.prototype.includes됩니다.
return this.indexOf(str) !== -1;
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를줍니다.
이것은 널리 지원됩니다.
for...in! , 그렇게 String.prototype.includes정의 하면 반복됩니다 .
문제:
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"));
Array.prototype.include()in javascript 를 계속 사용 하려면이 스크립트를 사용할 수 있습니다.
github-script-ie-include
IE를 감지하면 include ()를 match () 함수로 자동 변환합니다.
다른 옵션은 항상string.match(Regex(expression))
당신은 !! 및 ~ 연산자
var myString = 'this is my string';
!!~myString.indexOf('string');
// -> true
!!~myString.indexOf('hello');
// -> false
다음은 두 연산자 (!! 및 ~)에 대한 설명입니다.
https://www.joezimjs.com/javascript/great-mystery-of-the-tilde/