此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。

View in English Always switch to English

RegExp.prototype.hasIndices

基线 广泛可用

自 2021年9月 起,此特性已在主流浏览器中得到支持,可在大多数设备和浏览器版本中正常使用。

hasIndices 访问器属性指示 d 标志是否与正则表达式一起使用。

尝试一下

const regex1 = new RegExp("foo", "d");

console.log(regex1.hasIndices);
// Expected output: true

const regex2 = new RegExp("bar");

console.log(regex2.hasIndices);
// Expected output: false

描述

如果 d 标志被使用,则 RegExp.prototype.hasIndices 的值是 true;否则是 falsed 标志表示正则表达式匹配的结果应该包含每个捕获组子字符串开始和结束的索引。它不会以任何方式改变正则表达式的解释或匹配行为,它只在匹配的结果中提供额外的信息。

hasIndices 的 set 访问器是 undefined。你不能直接修改此属性。

示例

使用 hasIndices

js
const str1 = "foo bar foo";

const regex1 = /foo/dg;

console.log(regex1.hasIndices); // Output: true

console.log(regex1.exec(str1).indices[0]); // Output: Array [0, 3]
console.log(regex1.exec(str1).indices[0]); // Output: Array [8, 11]

const str2 = "foo bar foo";

const regex2 = /foo/;

console.log(regex2.hasIndices); // Output: false

console.log(regex2.exec(str2).indices); // Output: undefined

规范

规范
ECMAScript® 2027 Language Specification
# sec-get-regexp.prototype.hasIndices

浏览器兼容性

参见