javascript RegExp.$1 有什么作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18362164/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 11:37:56  来源:igfitidea点击:

What does RegExp.$1 do

javascriptregex

提问by Graham

I have come across a piece of code in JScript:

我在 JScript 中遇到过一段代码:

RegExp.

Does anybody know what it does?

有人知道它是做什么的吗?

If I output it on its own, I get nothing not even an error.

如果我自己输出它,我什么也得不到,甚至没有错误。

I can't find any reference to it at http://www.w3schools.com/jsref/jsref_obj_regexp.asp

我在http://www.w3schools.com/jsref/jsref_obj_regexp.asp 上找不到任何对它的引用

There was a regex search just prior to this piece of code, which I suspect has something to do with it:

在这段代码之前有一个正则表达式搜索,我怀疑这与它有关:

.search(/^__product\[(.+)\]$/)

回答by Rocket Hazmat

The literal expression RegExp.$1will get you the value of the first capture group of the last regex ran. Whatever that regex was.

文字表达式RegExp.$1将为您提供上次运行的正则表达式的第一个捕获组的值。不管那个正则表达式是什么。

For example:

例如:

var match = /_(.*)_/.exec('_test_');

var newMatch = '123-abc'.match(/(\d*)-(\w*)/);
var num = RegExp.; // '123';

RegExp.$1is globally available, so it can be accessed from anywhere in your page, regardless of where the regex itself was ran.

RegExp.$1是全局可用的,因此可以从页面中的任何位置访问它,无论正则表达式本身在哪里运行。

I've never seen this syntax used before seeing this question, and I wouldn't suggest using it, as I cannot find documentation on it. Also, anyregex ran on your page, regardless of where, will modify this property. If you want to get the capture groups, I'd use the arrays returned from String.matchor RegExp.execinstead.

在看到这个问题之前,我从未见过使用过这种语法,我不建议使用它,因为我找不到有关它的文档。此外,在您的页面上运行的任何正则表达式,无论在哪里,都会修改此属性。如果您想获取捕获组,我将使用从String.match或返回的数组RegExp.exec

EDIT: I found some documentation about this: http://msdn.microsoft.com/en-us/library/ie/24th3sah(v=vs.94).aspx

编辑:我找到了一些关于此的文档:http: //msdn.microsoft.com/en-us/library/ie/24th3sah(v=vs.94).aspx

EDIT 2: I found some more info about this: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Deprecated_and_obsolete_features#RegExp_Properties

编辑 2:我找到了一些关于此的更多信息:https: //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Deprecated_and_obsolete_features#RegExp_Properties

RegExp.$1is deprecated. That means future browsers might remove this "feature", so I suggest not using it.

RegExp.$1已弃用。这意味着未来的浏览器可能会删除此“功能”,因此我建议不要使用它。

回答by David Starkey

$1will return the first group that matches a regular expression.

$1将返回匹配正则表达式的第一个组。

In your example, the value stored in $1is whatever was matching .+

在您的示例中,存储的值$1是匹配的值.+

The group is denoted by parenthesis and you can have multiples. Each saved group will just increment the digit with the $, i.e. $1, $2, $3...

该组由括号表示,您可以有多个。每个保存的组只会用 增加数字$,即$1, $2, $3...

Example:

例子:

If your input was __product[Version 12 Coupe]then $1would contain Version 12 Coupe

如果您的输入是__product[Version 12 Coupe]那么$1将包含Version 12 Coupe

回答by j08691

These work in conjunction with capturing parentheses. For example, /(foo)/matches and remembers 'foo' in "foo bar." The matched substring can be recalled from the resulting array's elements [1], ..., [n] or from the predefined RegExp object's properties $1, ..., $9.

这些与捕获括号一起使用。例如,/(foo)/匹配并记住“foo bar”中的“foo”。匹配的子串可以从结果数组的元素 [1], ..., [n] 或预定义的 RegExp 对象的属性 $1, ..., $9 中调用。

In your example, the $1refers to the match made by (.+)

在您的示例中,$1指的是由(.+)

See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp

请参阅https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp

回答by Halcyon

$1is whatever is matched in the first capture. If you have more captures you can use $2, $3etc.

$1是第一次捕获中匹配的任何内容。如果您有更多的捕捉就可以使用$2$3等等。

Ex:

前任:

"abc".replace(/(.)/, ""); // aabc
"abc".replace(/(.{2})/, ""); // ababc
"abc".replace(/(.)(.)/, ""); // bac

回答by Mr. X

Like stated earlier, these constructs are called capturing parentheses groups and they're primarily used to reference the matches saved/remembered following the successful execution of an RegExp related operation.

如前所述,这些构造称为捕获括号组,它们主要用于引用成功执行 RegExp 相关操作后保存/记住的匹配项。

Also, like stated earlier, I don't advise using any of them in your code unless a well controlled environment/context is secured, like the one provided by the native replace()method found on the String.prototypeobject.

此外,如前所述,我不建议在您的代码中使用它们中的任何一个,除非受到良好控制的环境/上下文是安全的,就像replace()String.prototype对象上找到的本机方法提供的那样。

回答by eyecatchUp

To add some details:

添加一些细节:

(As already said,) the RegExp.$n-property (where nis a single digit 1-9) returns the last nthparenthesized (captured) substring in a match.

(正如已经说过的,)的RegExp.$n-property(其中n是一位数字1-9)返回最后一个n括号(捕获)中的匹配子串。

These properties were first implemented in JavaScript 1.2 and deprecated in JavaScript 1.5 - when RegExpunderwent a major change and many of the results from RegExp.prototype.exec(string)were moved from the RegExpobject to the RegExpinstance and all the .$properties (and their full name versions (except for .multiline)) "went away".

这些属性首先在 JavaScript 1.2 中实现并在 JavaScript 1.5 中弃用 - 当RegExp经历了重大变化并且许多结果从对象RegExp.prototype.exec(string)移动RegExpRegExp实例和所有.$属性(及其全名版本(除了.multiline))“离开”。



The non-standard1$1, $2, $3, $4, $5, $6, $7, $8, $9properties are staticand read-onlyproperties of regular expressions (that contain parenthesized substring matches) and are modified whenever successful matches are made.

所述非标准1$1$2$3$4$5$6$7$8$9属性是静态的只读的正则表达式的特性(包含括号内的字符串匹配),并且无论何时成功匹配是由改性

They are not a property of an individual regular expression object. Instead, you always use them as RegExp.$1, ..., RegExp.$9.

它们不是单个正则表达式对象的属性。相反,您总是将它们用作RegExp.$1, ..., RegExp.$9

The number of possible parenthesized substrings is unlimited (of course), but the RegExpobject can only hold the last nine.

可能的括号子串的数量是无限的(当然),但RegExp对象只能容纳最后九个。

1Non-standard = Not part of any current specification!

1非标准 = 不属于任何当前规范!



You can find the definition and references in the following sections of the ECMA-262 3Specs:

您可以在ECMA-262 3规范的以下部分找到定义和参考:

  • 15.5.4.10 - String.prototype.match(regexp)
  • 15.5.4.11 - String.prototype.replace(regexp)
  • 15.10.2.1 - The RegExp Object Notation's NCapturingParens
  • 15.10.6.2 - RegExp.prototype.exec(string)
  • 15.5.4.10 - String.prototype.match(regexp)
  • 15.5.4.11 - String.prototype.replace(regexp)
  • 15.10.2.1 - RegExp 对象符号的NCapturingParens
  • 15.10.6.2 - RegExp.prototype.exec(string)