RegExp.prototype.hasIndices

RegExp 实例的 hasIndices 访问器属性返回此正则表达式是否使用 d 标志。

¥The hasIndices accessor property of RegExp instances returns whether or not the d flag is used with this regular expression.

Try it

描述

¥Description

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

¥RegExp.prototype.hasIndices has the value true if the d flag was used; otherwise, false. The d flag indicates that the result of a regular expression match should contain the start and end indices of the substrings of each capture group. It does not change the regex's interpretation or matching behavior in any way, but only provides additional information in the matching result.

该标志主要影响 exec() 的返回值。如果存在 d 标志,则 exec() 返回的数组具有附加的 indices 属性,如 exec() 方法的 返回值 中所述。由于所有其他与正则表达式相关的方法(例如 String.prototype.match())在内部调用 exec(),因此如果正则表达式具有 d 标志,它们也将返回索引。

¥This flag primarily affects the return value of exec(). If the d flag is present, the array returned by exec() has an additional indices property as described in the exec() method's return value. Because all other regex-related methods (such as String.prototype.match()) call exec() internally, they will also return the indices if the regex has the d flag.

hasIndices 的集合访问器是 undefined。你不能直接更改此属性。

¥The set accessor of hasIndices is undefined. You cannot change this property directly.

示例

¥Examples

组和反向引用 > 使用组和匹配索引 有更详细的使用示例。

¥There's a more detailed usage example at Groups and backreferences > Using groups and match indices.

使用 hasIndices

¥Using hasIndices

js
const str1 = "foo bar foo";

const regex1 = /foo/dg;

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

console.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); // false

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

规范

Specification
ECMAScript Language Specification
# sec-get-regexp.prototype.hasIndices

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看