This page was translated from English by the community.Learn more and join the MDN Web Docs community.
RegExp.prototype.hasIndices
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2021년 9월.
RegExp 인스턴스의hasIndices 접근자 속성은 이 정규 표현식과 함께d 플래그가 사용되는지 여부를 반환합니다.
In this article
시도해 보기
const regex1 = new RegExp("foo", "d");console.log(regex1.hasIndices);// Expected output: trueconst regex2 = new RegExp("bar");console.log(regex2.hasIndices);// Expected output: false설명
d 플래그가 사용된 경우RegExp.prototype.hasIndices는true 값을 가지며, 그렇지 않은 경우false 값을 갖습니다.d 플래그는 정규 표현식 일치 결과에 각 캡처 그룹의 하위 문자열의 시작 및 끝 인덱스가 포함됨을 나타냅니다. 이 플래그는 정규 표현식의 해석이나 일치 동작을 어떤 식으로든 변경하지 않으며 일치 결과에 추가 정보만 제공합니다.
이 플래그는 주로exec()의 반환 값에 영향을 줍니다.d 플래그가 있는 경우,exec()가 반환하는 배열에는exec() 메서드의반환 값에 설명된 대로 추가indices 속성이 있습니다. 다른 모든 정규식 관련 메서드(예:String.prototype.match())는 내부적으로exec()을 호출하므로 정규식에d 플래그가 있는 경우 인덱스도 반환합니다.
hasIndices 설정 접근자는undefined입니다. 이 속성을 직접 변경할 수 없습니다.
예제
좀 더 상세한 사용 예제는그룹과 역참조 > 그룹과 인덱스 일치 사용하기에 있습니다.
hasIndices 사용하기
const str1 = "foo bar foo";const regex1 = /foo/dg;console.log(regex1.hasIndices); // trueconsole.log(regex1.exec(str1).indices[0]); // [0, 3]console.log(regex1.exec(str1).indices[0]); // [8, 11]const str2 = "foo bar foo";const regex2 = /foo/;console.log(regex2.hasIndices); // falseconsole.log(regex2.exec(str2).indices); // undefined명세서
| Specification |
|---|
| ECMAScript® 2026 Language Specification> # sec-get-regexp.prototype.hasIndices> |