简体   繁体   English

这个捕获组在 Javascript 的替换函数文档中的这个正则表达式中做了什么?

[英]What is this capture group doing in this regex expression in the replace function documentation in Javascript?

Here is the code:这是代码:

I think the following example will set newString to:我认为以下示例会将 newString 设置为:

'abc - 12345 - #$*%': 'abc - 12345 - #$*%':

function replacer(match, p1, p2, p3, offset, string) {
  // p1 is nondigits, p2 digits, and p3 non-alphanumerics
  return [p1, p2, p3].join(' - ');
}

var newString = 'abc12345#$*%'.replace(/([^\d]*)(\d*)([^\w]*)/, replacer);

I am unsure what the 3 capture groups are doing in the regex expression?我不确定 3 个捕获组在正则表达式中做什么? Does the first parathesis:是否第一个拟态:

([^/d]*)

capture everything that is not a digit?捕捉所有不是数字的东西?

Does the second group capture everything that is a digit?第二组是否捕获所有数字?

Does the third group capture everything that is not an alphanumeric character (letter or number)?第三组是否捕获了不是字母数字字符(字母或数字)的所有内容?

What gets passed into the replacer function?什么被传递到替换函数中? What would be p1 p2 and p3 if the replacer function in the replace function doesn't receive any arguments?如果替换函数中的替换函数没有接收任何参数,那么 p1、p2 和 p3 会是什么?

I often use https://regex101.com/ and https://www.debuggex.com/ for explaining regular expressions.我经常使用https://regex101.com/https://www.debuggex.com/来解释正则表达式。

This is how Regex101 explains expression in question这就是 Regex101 解释相关表达式的方式正则表达式101

And this is how Debuggex explains expression in question这就是 Debuggex 解释相关表达式的方式正则表达式可视化

Debuggex调试器

Also if you debug this in Chrome console this is what you should get此外,如果您在 Chrome 控制台中调试它,这就是您应该得到的铬合金

I hope this helps you understand this better我希望这可以帮助您更好地理解这一点

The pattern ([^\\d]*)(\\d*)([^\\w]*) is being used to match three segments of your input string abc12345#$*% .模式([^\\d]*)(\\d*)([^\\w]*)用于匹配输入字符串abc12345#$*%三个部分

  1. ([^\\d]*) - matches any non-digit zero or more times using negation [^] , in this case abc . ([^\\d]*) - 使用否定[^]匹配任何非数字零次或多次,在本例中为abc
  2. (\\d*) - matches zero or more digits [0-9] following the previous capture group, in this case 12345 . (\\d*) - 匹配前一个捕获组之后的零个或多个数字[0-9] ,在本例中为12345
  3. ([^\\w]*) - matches any character not in the set [A-Za-z0-9_] following the previous capture group, so pretty much special characters. ([^\\w]*) - 匹配前一个捕获组之后不在集合[A-Za-z0-9_]中的任何字符,所以非常特殊的字符。 In this case #$*% .在这种情况下#$*%

Your replacer function is not actually replacing those capture groups, but preserving them and putting dashes - between them.你的replacer的功能实际上没有取代那些捕获组,但保存它们,并把破折号-他们之间。

Because there are three capture groups (as explained above), the three params p1 - p3 represent those.因为有三个捕获组(如上所述),三个参数p1 - p3代表那些。

The function params are dynamically populated based on the number of capture groups in your pattern (part of the laid-back nature of JavaScript).函数参数是根据您的模式中的捕获组数量动态填充的(JavaScript 悠闲性质的一部分)。 So if you had 4 capture groups, there would be four capture group params:因此,如果您有 4 个捕获组,则将有四个捕获组参数:

function replacer(match, p1, p2, p3, p4, offset, string) { ... }

And the param names obviously aren't significant, just whatever you want to call them in your function.参数名称显然并不重要,只是您想在函数中调用它们的任何名称。

The replacer function takes the three capture group params and returns the following: [p1, p2, p3].join(' - ') . replacer函数采用三个捕获组参数并返回以下内容: [p1, p2, p3].join(' - ') This just puts - (with spaces on either side) between the capture groups and returns abc - 12345 - #$*% .这只是在捕获组之间放置- (两侧有空格)并返回abc - 12345 - #$*%

Further reading on the join function: Array.prototype.join() .进一步阅读join函数: Array.prototype.join()

  1. ([^\\d]*) matches 0 or more non . ([^\\d]*)匹配 0 个或多个non It could be written as ([\\D]*)可以写成([\\D]*)
  2. (\\d*) matches 0 or more digits (\\d*)匹配 0 个或多个数字
  3. ([^\\w]*) matches 0 or more non characters. ([^\\w]*)匹配 0 个或多个非字符。 It could be written as ([\\W]*)可以写成([\\W]*)
  4. The parameters passed to the replacer function are: the matched substring, every captured group (if nothing is captured an empty string is passed), the offset that has to be examinated of the string (last parameter)传递给替换函数的参数是:匹配的子字符串,每个捕获的组(如果没有捕获到空字符串),必须检查的字符串的偏移量(最后一个参数)

For a better explaination see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace有关更好的解释,请参阅: https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace

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

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