gpt4 book ai didi

javascript - 无法在 JavaScript 中删除 RegExp.$1

转载 作者:搜寻专家 更新时间:2023-10-31 23:38:59 24 4
gpt4 key购买 nike

有人知道为什么 RegExp.$1-x 变量不能设置为未定义吗?

以下是 nodejs 中的示例。 chrome/firefox 中的相同行为:

console.log('1:', RegExp.$1, RegExp.$2, RegExp.$3, RegExp.$4); 
// 1: / Users/tiefenma/Desktop/ test.js .js

var string = 'test1 test2 test3';
var test = string.match(/(test1).*(test2).*(test3)/gm);


console.log('2:', RegExp.$1, RegExp.$2, RegExp.$3, RegExp.$4);
// 2: test1, test2, test3


RegExp.$1 = undefined;
RegExp.$2 = undefined;
RegExp.$3 = undefined;
console.log('3:', RegExp.$1, RegExp.$2, RegExp.$3, RegExp.$4);
// 3: test1, test2, test3


delete RegExp.$1;
delete RegExp.$2;
delete RegExp.$3;
console.log('4:', RegExp.$1, RegExp.$2, RegExp.$3, RegExp.$4);
// 4: test1, test2, test3


''.match(/(.*)/g);
console.log('5:', RegExp.$1, RegExp.$2, RegExp.$3, RegExp.$4);
// 5:

最佳答案

Object.<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyDescriptor" rel="noreferrer noopener nofollow">getOwnPropertyDescriptor</a>(RegExp, "$1")将为您提供有关 $1 的信息:

Chrome 30:

{ 
get: function,
set: function,
enumerable: true,
<b>configurable: false</b>
}

Firefox 22:

{
    <b>configurable: false,</b>
    enumerable: true, 
    value: "", 
    <b>writable: false</b>
}

请注意 configurablefalse ;所以,你不能删除该属性。此外,当 writablefalse , 你不能赋值。

configurable
true if and only if the type of this property descriptor may be changed and if the property may be deleted from the corresponding object.

writable
True if and only if the value associated with the property may be changed with an assignment operator. Defaults to false.

因此,在 Chrome 和 Firefox 中,实现会阻止您删除 $1或为 $1 赋值与 = .

作为比较点,IE10 报告的内容略有不同......

Internet Explorer 10:

{
    value: "",
    <b>writable: true,</b>
    enumerable: false,
    <b>configurable: true</b>
}

也就是说,IE10 仍然不允许您删除该属性或为其赋值。去图吧。

关于javascript - 无法在 JavaScript 中删除 RegExp.$1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17995015/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com