简体   繁体   English

正则表达式中的$ 0和$ 1

[英]$0 and $1 in Regular Expression

I am trying to use replace with regular expression and need some help understanding what $1 means in this code. 我正在尝试使用带有正则表达式的replace,并且需要一些帮助来理解$ 1在此代码中的含义。 This code is to swap the case. 这段代码是用来交换大小写的。

return str.replace(/([a-z])|([A-Z])/g,
    function($0, $1)
    { return ($1) ? $0.toUpperCase() : $0.toLowerCase(); })

I understand in the first parameter of replace method I check if we have lowercase or uppercase alphabet, but I do not understand how and what it does to have the second parameter. 我知道在replace方法的第一个参数中,我会检查我们是否具有小写或大写字母,但是我不明白具有第二个参数的方式和作用。

I understand the syntax in which if ($1) is true we execute $0.toUpperCase(), if not we do $0.toLowerCase(). 我理解如果($ 1)为true的语法,我们执行$ 0.toUpperCase(),否则执行$ 0.toLowerCase()。 But how to decide from what if ($1) is true? 但是,如何从($ 1)为真来决定呢? What condition does ($1) have? ($ 1)有什么条件? I think I understand $0 is for the entire matched string. 我想我明白$ 0是整个匹配的字符串。 But I am confused with ($1). 但是我对($ 1)感到困惑。 Thanks! 谢谢!

What condition does ($1) have? ($ 1)有什么条件?

None -- regular expression syntax doesn't specify how match groups are to be evaluated for truthiness, so this depends on the behavior of the containing language. 无-正则表达式语法没有指定如何评估匹配组的真实性,因此这取决于所包含语言的行为。

I'm guessing that, in this case, it's evaluating empty strings as false, and non-empty strings as true -- but as you haven't told us what language this is, one can't tell for certain. 我猜想,在这种情况下,它会将空字符串评估为假,将非空字符串评估为真-但由于您没有告诉我们这是什么语言,因此无法确定。

$0 is indeed the entire matched string. $0确实是整个匹配的字符串。 $1 is the first subpattern (ie. the lowercase letter). $1是第一个子模式(即小写字母)。 If the first subpattern matched, we uppercase it, otherwise we lowercase it. 如果第一个子模式匹配,则将其大写,否则将其小写。 It should be noted that the function is given another argument, but it is not used in this case. 应该注意的是,该函数被赋予了另一个参数,但是在这种情况下不使用它。

Note the parameters passed to the evaluator . 注意传递给评估器的参数 Going with the pattern given, I've rewritten it as: 按照给定的模式,我将其重写为:

str.replace(/([a-z])|([A-Z])/g,
  function(match, p1, p2) {
    return p1 ? p1.toUpperCase() : p2.toLowerCase();
  })

If ([az]) matches then the bound p1 ( $1 ) variable will evaluate to a truthy-string (any string that is not empty; in particular, one that was accepted by the regular expression); 如果([az])匹配,则绑定的p1$1 )变量将求值为真字符串(任何不为空的字符串;特别是正则表达式接受的字符串); otherwise p1 will be the empty string "" (which is a falsy-value). 否则, p1将为空字符串"" (这是一个伪造的值)。 This is why the check on p1 ( $1 ) is correct - note that the bound capture groups always have the type of string. 这就是为什么对p1$1 )的检查正确的原因-请注意,绑定的捕获组始终具有字符串类型。

Note that thee is no point to check on match ( $0 ) as it will never be anything-but-truthy with the regular expression (it will be either the first or second alternation subexpression). 请注意,您没有必要检查match$0 ),因为它永远不会是什么,而是带有正则表达式的真实(它将是第一个或第二个替换子表达式)。

$0 and $1 in this example are simply variables (named using Hungarian Notation). 在此示例中,$ 0和$ 1只是变量(使用匈牙利符号表示)。 You can easily replace them with a and b and the code works just the same. 您可以轻松地用a和b替换它们,并且代码的工作原理相同。 The function is called with many parameters. 该函数用许多参数调用。 The first parameter is the matched substring. 第一个参数是匹配的子字符串。 The second parameter is the first capture (anything that matched the regex in the first parentheses, in this case a single lowercase letter az). 第二个参数是第一个捕获(与第一个括号中的正则表达式匹配的任何字符,在这种情况下为单个小写字母az)。 The third (missing) parameter would be the second capture (matching uppercase letter AZ), but note this is ignored. 第三个(丢失)参数将是第二个捕获(匹配大写字母AZ),但请注意,此参数将被忽略。

Because the regex using the global "g" flag, the function is called (potentially) many times. 因为正则表达式使用全局“ g”标志,所以该函数(可能)多次调用。 The regex matches any lower or uppercase letter. 正则表达式匹配任何小写或大写字母。 The first function parameter will match each character in succession from str, and the second parameter will be set only if the first capture group was matched - meaning the second function parameter is set only if the character is lowercase . 第一个功能参数将从str开始连续匹配每个字符,而第二个参数仅在第一个捕获组匹配时设置-这意味着第二个功能参数仅在字符为小写时设置 If the second parameter was set (ie this is a lowercase character), then the toUpperCase function is called on the match. 如果设置了第二个参数(即,这是一个小写字符),则在匹配项上调用toUpperCase函数。 If the second parameter is unset (ie this is an uppercase character), then the toLowerCase function is called on the match. 如果未设置第二个参数(即,这是一个大写字符),则在匹配项上调用toLowerCase函数。 In this latter case the unused third parameter would have contained the second capture group contents. 在后一种情况下,未使用的第三参数包含第二捕获组内容。

The entire solution has the effect of swapping cases on a character-by-character basis. 整个解决方案具有逐个字符交换大小写的效果。

But.. the use of $0 and $1 in this code suggest the creator was toying with something else: references and back-references. 但是..在此代码中使用$ 0和$ 1意味着创建者在玩弄其他东西:引用和反向引用。 $0 (or \\0 in some languages) is a reference to the match (ie it is precisely the first argument to the function), $1 (or \\1) is a reference to the first capture group, etc. The author of this code named the variables as such to support the notion that the first argument is equivalent to $0 and the second argument to $1, etc. This is, in my opinion, enlightened and entirely confusing. $ 0(或某些语言中的\\ 0)是对匹配项的引用(即,它恰好是该函数的第一个参数),$ 1(或\\ 1)是对第一个捕获组的引用,以此类推。如此命名变量以支持第一个参数等效于$ 0,第二个参数等效于$ 1的概念,依此类推。在我看来,这是开明的,并且完全令人困惑。 The use of these variable names suggests something magic is happening. 这些变量名的使用表明发生了一些不可思议的事情。 The use of Hungarian Notation also implies something else. 匈牙利表示法的使用还意味着其他含义。 There is nothing magic here, and as such the variables should be more simply named - match and is_lowercase would have been fine. 这里没有什么魔术,因此变量应该更简单地命名-match和is_lowercase会很好。

Furthermore, since the third function argument is missing, there is no need for the uppercase character class to be captured. 此外,由于缺少第三个函数自变量,因此无需捕获大写字符类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM