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。离线整理了版式、链接和媒体显示。
