현대 엔진에서 당신은 String.prototype.includes 사용할 수 있습니다
str.includes(substr)
는 이전 버전의 브라우저에서 작동하고 String.prototype.indexOf을 방지 무언가를 원하는 경우에, 당신은 문자열을 통해 수동 루프에 의해, includes
을 구현할 수 있습니다 : 당신이 탈출을 사용할 수있는 정규식 테스트를 사용하는 방법에 대한 끈질긴 경우
String.prototype.includes = function (substr, start) {
var i = typeof start === 'number' ? start : 0;
// Condition exists early if there are not enough characters
// left in substr for j to ever reach substr.length
for (var j = 0; i + substr.length < this.length + j + 1; ++i) {
j = substr[j] === this[i] ? j + 1 : 0;
if (j === substr.length)
return true;
}
return false;
};
함수를 this answer에서 올바르게 정규 표현식에서 사용할 substr을 이스케이프 처리합니다. 이 경우에는 (
및 )
전에 백 슬래시 추가 :
new RegExp(substr.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')).test(str)
만 문자열이 문자열,'할이 포함되어 있는지 확인하려면를!, 또는 더 나은 ES6에 ~ str.indexOf (SUBSTR)' ,'str.contains (substr)'. –
실제로 무엇을하려고합니까? – bipen
@Derek 朕 會 功夫 내가 javascript doen't 어떤 방법을 가지고있는 것 같아요 출력을 보여줄 수 있습니까? – Mayank