这个正则表达式的作用是什么,函数的作用是什么?

时间:2022-02-09 01:58:47

I have come across the following regular expression in a piece of client side javascript:

我在一段客户端javascript中遇到了以下正则表达式:

([^?=&]+)(=([^&]*))?

When I run it through a regular expression tester (firefox add-on), I get the values of the query string of a url highlighted in one colour, the question mark and ampersands not highlighted at all and the rest in another colour.

当我在一个正则表达式测试器(firefox扩展)中运行它时,我得到一个用一种颜色突出显示的url的查询字符串的值,问号和&完全没有突出显示,其余的则用另一种颜色。

I'm not sure whether it is matching one colour or the other or both, but then I use the replace option and nothing gets replaced.

我不确定它是匹配一种颜色还是另一种颜色还是两者都匹配,但是我使用了替换选项,没有任何东西被替换。

That's only the beginning of my question. The piece of code in full is this:

这只是我问题的开始。完整的代码是:

var linkObj = new Object();
jQuery(this).attr('href').replace(
    new RegExp( "([^?=&]+)(=([^&]*))?", "g" ),
    function( jQuery0, jQuery1, jQuery2, jQuery3 ){
        linkObj[ jQuery1 ] = jQuery3;
    }
);

What are those 4 parameters in the function (jQuery0, jQuery1, jQuery2, jQuery3)?

函数中的这4个参数是什么(jQuery0、jqueryy1、jQuery2、jQuery3)?

Any help would be appreciated.

如有任何帮助,我们将不胜感激。

Thank you.

谢谢你!

1 个解决方案

#1


3  

Appears to break down the GET portion of a URL.

似乎要分解URL的GET部分。

([^?=&]+)      # 1 or more characters, exclusive of ?, = or &
(=             # = sign
  ([^&]*)        # any character not a &, 0 or more times
)?             # but optional

The function is a callback which replace can accept with regular expression calls. Each argument is a different value found within the regex match. The function can then return the result that the replace method should substitute with). So, given ?foo=bar:

函数是一个回调,它可以用正则表达式调用来代替。每个参数都是regex匹配中发现的不同值。然后,函数可以返回替换方法应该替换为)的结果。所以,鉴于? foo = bar:

  • jQuery0 - full match
  • jQuery0——完全匹配
  • jQuery1 - First capture (e.g. foo)
  • jQuery1 -第一次捕获(例如foo)
  • jQuery2 - Second capture (=bar)
  • jQuery2 -第二次捕获(=bar)
  • jQuery3 - Third (nested) capture (bar)
  • jQuery3 -第三(嵌套)捕获(bar)

#1


3  

Appears to break down the GET portion of a URL.

似乎要分解URL的GET部分。

([^?=&]+)      # 1 or more characters, exclusive of ?, = or &
(=             # = sign
  ([^&]*)        # any character not a &, 0 or more times
)?             # but optional

The function is a callback which replace can accept with regular expression calls. Each argument is a different value found within the regex match. The function can then return the result that the replace method should substitute with). So, given ?foo=bar:

函数是一个回调,它可以用正则表达式调用来代替。每个参数都是regex匹配中发现的不同值。然后,函数可以返回替换方法应该替换为)的结果。所以,鉴于? foo = bar:

  • jQuery0 - full match
  • jQuery0——完全匹配
  • jQuery1 - First capture (e.g. foo)
  • jQuery1 -第一次捕获(例如foo)
  • jQuery2 - Second capture (=bar)
  • jQuery2 -第二次捕获(=bar)
  • jQuery3 - Third (nested) capture (bar)
  • jQuery3 -第三(嵌套)捕获(bar)