RegExp.prototype.global

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

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

Try it

描述

¥Description

如果使用了 g 标志,则 RegExp.prototype.global 的值为 true;否则,falseg 标志指示应针对字符串中所有可能的匹配项来测试正则表达式。每次调用 exec() 都会更新其 lastIndex 属性,以便下一次调用 exec() 将从下一个字符开始。

¥RegExp.prototype.global has the value true if the g flag was used; otherwise, false. The g flag indicates that the regular expression should be tested against all possible matches in a string. Each call to exec() will update its lastIndex property, so that the next call to exec() will start at the next character.

某些方法(例如 String.prototype.matchAll()String.prototype.replaceAll())将验证如果参数是正则表达式,则它是全局的。当正则表达式是全局的时,正则表达式的 @@match@@replace 方法(由 String.prototype.match()String.prototype.replace() 调用)也会有不同的行为。

¥Some methods, such as String.prototype.matchAll() and String.prototype.replaceAll(), will validate that, if the parameter is a regex, it is global. The regex's @@match and @@replace methods (called by String.prototype.match() and String.prototype.replace()) would also have different behaviors when the regex is global.

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

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

示例

¥Examples

使用全局

¥Using global

js
const regex = /foo/g;
console.log(regex.global); // true

const str = "fooexamplefoo";
const str1 = str.replace(regex, "");
console.log(str1); // example

const regex1 = /foo/;
const str2 = str.replace(regex1, "");
console.log(str2); // examplefoo

规范

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

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看