且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

学习正则表达式和jQuery-.match返回什么?

更新时间:2022-03-23 22:29:04

尝试一下:

var x = $(this)[0].match(/[^\?]*$/);
alert(x[0]);

假设$(this)[0]返回"/my/fun/path?query1 = a& query2 = b 请注意,x将是数组["query1=a&query2=b"]

Pretending that $(this)[0] returns "/my/fun/path?query1=a&query2=b Note that x will be the array ["query1=a&query2=b"]

例如,如果您想获取每个查询参数作为数组中的索引,则可以执行以下操作:

If you, for instance, wanted to get each query parameter as an index in an array you could do this:

var params = $(this)[0].match(/[^\?]*$/)[0].split('&');

哪个会给你:

["query1=a", "query2=b"]

编辑为您分解正则表达式:

Editing to break-down the regex for you:

  • []是一个字符类,并且只能匹配一个字符,无论它包含多少内容.
  • 例如,
  • [a-z]将与任何单个小写字母字符匹配.
  • 字符类开头的^(脱字符)是一个非标识符.指示此字符类具有相反的效果-它将与不与其余内容匹配的任何单个字符匹配.此位置(字符类中的第一个字符)是插入号唯一指示否"的时间.在其他情况下,它具有不同的含义(例如,在正则表达式的开头,^将指示字符串的开头必须从此处开始).
  • 因此,我们的字符类[^\?]表示我们将匹配不是问号的任何单个字符
  • 星号始终表示零个或多个前面的语句",因此当您尝试仅使用*表示此处的任何内容"时,您实际上所做的就是复制\?零次或多次-向解释器说我要问号零次或多次".您的alert(x[0]) 应该输出了一个空对话框,稍后会更多
  • 在我们的示例中,星号正在检查任何非问号(零次或多次).
  • reg-ex末尾的$表示字符串末尾",这意味着……当然,字符串的末尾必须在该点.
  • The [] is a character class, and can only ever match one single character no matter how much contents it has.
  • [a-z] for instance, will match any single lower-case alphabetic character.
  • The ^ (caret) at the beginning of the character class is a not-identifier. Indicating that this character class has an opposite effect - it will match any single character that does NOT match the rest of the contents. This position (first character in a character class) is the only time that the caret indicates 'not'. In other contexts it has a different meaning (For instance, at the beginning of your regex the ^ will indicate that the beginning of the string must start there).
  • Thus, our character class [^\?] indicates that we will match any single character that is not a question mark
  • The asterisk always means "zero or more of the preceding statement", so when you were trying to just use * to mean "anything here" what you were actually doing was duplicating the \? zero or more times - stating to the interpreter "I want a question mark zero or more times". Your alert(x[0]) should have output an empty dialog, more on that later
  • In our case the asterisk is checking for any non-question mark, zero or more times.
  • The $ at the end of the reg-ex states "End of string" which means... of course, that the end of the string must be at that point.

因此,我们知道正则表达式将不会从您的url中收集问号,因为这样当它仍然收集多个非问号时,它将无法命中字符串的末尾.除非另外指定,否则*总是贪婪的,因此它将获得尽可能多的字符.

Therefore we know the regex will not collect the question mark from your url because then it would not be able to hit the end of the string while still collection multiple non-question marks. The * is always greedy unless otherwise specified, so it will get as many characters as possible.

您的alert(x [0])应该已经输出了一个空警报(我对其进行了测试,它确实对我有用),因为.match()方法匹配了它可以做到的第一件事,因为/\?*/实际上匹配了每个单个对象可能是一个字符串,它必须在看到第一个字符并说哦,这里有比赛!"之后就停止了.

Your alert(x[0]) should have output an empty alert (I tested it, and it did for me) because the .match() method matches the first thing it can, since /\?*/ matches virtually every single string possible it must have stopped after seeing the first character and saying "oh there's a match here!"

希望这对您的理解有所帮助:)正则表达式在您入门之初可能会感到困惑,但我绝对喜欢它们.如此强大.

Hopefully this was helpful for your understanding :) Regexes can be confusing when you start out with them but I absolutely love 'em. So powerful.