大网数据广告活动
Web 标准与前端开发

RegExp.prototype.hasIndices

· 52CE 我爱测

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

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

JavaScript Demo: RegExp.prototype.hasIndices

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;否则是 false。d 标志表示正则表达式匹配的结果应该包含每个捕获组子字符串开始和结束的索引。它不会以任何方式改变正则表达式的解释或匹配行为,它只在匹配的结果中提供额外的信息。

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

示例

使用 hasIndices

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

规范

浏览器兼容性

参见

  • RegExp.prototype.lastIndex
  • RegExp.prototype.exec()
  • RegExp.prototype.dotAll
  • RegExp.prototype.global
  • RegExp.prototype.ignoreCase
  • RegExp.prototype.multiline
  • RegExp.prototype.source
  • RegExp.prototype.sticky
  • RegExp.prototype.unicode

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。