I am trying to use replace with regular expression and need some help understanding what
I am trying to use replace with regular expression and need some help understanding what $1 means in this code. This code is to swap the case.
我正在尝试使用正则表达式替换,并需要一些帮助来理解$ 1在此代码中的含义。此代码用于交换案例。
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.
我理解在替换方法的第一个参数中我检查我们是否有小写或大写字母,但我不明白如何以及如何使用第二个参数。
I understand the syntax in which if ($1) is true we execute $0.toUpperCase(), if not we do $0.toLowerCase(). But how to decide from what if ($1) is true? What condition does ($1) have? I think I understand $0 is for the entire matched string. But I am confused with ($1). Thanks!
我理解if($ 1)为真的语法,我们执行$ 0.toUpperCase(),否则我们执行$ 0.toLowerCase()。但是如何判断if($ 1)是真的呢? (1美元)有什么条件?我想我理解$ 0是整个匹配的字符串。但我很困惑($ 1)。谢谢!
4 个解决方案
#1
2
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.
我猜测,在这种情况下,它将空字符串评估为false,将非空字符串评估为true - 但是由于您没有告诉我们这是什么语言,所以无法确定。
#2
1
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 ([a-z]) 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); otherwise p1 will be the empty string "" (which is a falsy-value). This is why the check on p1 ($1) is correct - note that the bound capture groups always have the type of string.
如果([a-z])匹配,则绑定的p1($ 1)变量将计算为truthy-string(任何非空的字符串;特别是正则表达式接受的字符串);否则p1将是空字符串“”(这是一个假值)。这就是对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).
请注意,你没有必要检查匹配($ 0),因为它永远不会是正则表达式(它将是第一个或第二个交替子表达式)。
#3
0
$0 is indeed the entire matched string. $1 is the first subpattern (ie. the lowercase letter). 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.
$ 0确实是整个匹配的字符串。 $ 1是第一个子模式(即小写字母)。如果第一个子模式匹配,我们将其大写,否则我们将其小写。应该注意的是,该函数有另一个参数,但在这种情况下不使用它。
#4
0
$0 and $1 in this example are simply variables (named using Hungarian Notation). You can easily replace them with a and b and the code works just the same. 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 a-z). The third (missing) parameter would be the second capture (matching uppercase letter A-Z), but note this is ignored.
此示例中的$ 0和$ 1只是变量(使用匈牙利表示法命名)。您可以使用a和b轻松替换它们,代码的工作原理相同。使用许多参数调用该函数。第一个参数是匹配的子字符串。第二个参数是第一个捕获(与第一个括号中的正则表达式匹配的任何内容,在本例中是单个小写字母a-z)。第三个(缺失)参数将是第二个捕获(匹配大写字母A-Z),但请注意这将被忽略。
Because the regex using the global "g" flag, the function is called (potentially) many times. 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. If the second parameter was set (i.e. this is a lowercase character), then the toUpperCase function is called on the match. If the second parameter is unset (i.e. this is an uppercase character), then the toLowerCase function is called on the match. In this latter case the unused third parameter would have contained the second capture group contents.
因为正则表达式使用全局“g”标志,所以该函数被(可能)多次调用。正则表达式匹配任何小写或大写字母。第一个函数参数将匹配str中的每个字符,第二个参数仅在第一个捕获组匹配时设置 - 这意味着仅当字符为小写时才设置第二个函数参数。如果设置了第二个参数(即这是一个小写字符),则在匹配时调用toUpperCase函数。如果第二个参数未设置(即这是一个大写字符),则在匹配时调用toLowerCase函数。在后一种情况下,未使用的第三参数将包含第二捕获组内容。
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 (or \0 in some languages) is a reference to the match (i.e. 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. 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.
但是..在这段代码中使用$ 0和$ 1表明创建者正在玩弄其他东西:引用和反向引用。 $ 0(或某些语言中的\ 0)是对匹配的引用(即它恰好是函数的第一个参数),$ 1(或\ 1)是对第一个捕获组的引用,等等。此代码的作者将这些变量命名为支持第一个参数等价于$ 0而第二个参数等于$ 1等概念。在我看来,这是开明的,完全令人困惑的。使用这些变量名称表明正在发生一些神奇的事情。使用匈牙利表示法也意味着别的东西。这里没有什么神奇之处,因此变量应该更简单命名 - 匹配和is_lowercase就可以了。
Furthermore, since the third function argument is missing, there is no need for the uppercase character class to be captured.
此外,由于缺少第三个函数参数,因此不需要捕获大写字符类。
means in this code. This code is to swap the case.I am trying to use replace with regular express