-6

What is the difference in what these 2 are doing? Thx

 var m = document.referrer.match(/\&cd=([\d]*)/);

and

var m = document.referrer.match(/cd=(.*?)&/);

Which one is more efficient and effective?

user2022284
  • 333
  • 1
  • 6
  • 16
  • 8
    Effective for what ? They don't do the same thing. – Denys Séguret Mar 18 '13 at 16:49
  • 2
    they both want a url with a `cd` parameter in the query portion, but one requires digits and have other query paramaters before it. the other allows ANY values, but requires another query paramter after it. e.g. they're related, but definitely NOT the same. – Marc B Mar 18 '13 at 16:50
  • 1
    The most efficient and effective one is the one that gets you the right answer the quickest. Without knowing what it is your trying to match who knows. – Dave Sexton Mar 18 '13 at 16:52
  • Using this to extract a numeric value from the cd parameter of a url string. the cd parameter is one of several parameters in the string. I am looking for the one that effectively extracts the value in a wide range of scenarios. btw - which one requires digits and which one allows any values ? – user2022284 Mar 18 '13 at 16:53
  • Have to wonder where you found these regexes. Perhaps this will help you understand what they mean: http://regexlib.com/CheatSheet.aspx – femtoRgon Mar 18 '13 at 16:58

1 Answers1

3

/\&cd=([\d]*)/ - matches any string starting with an "&cd=" followed by any zero or more decimal digits. The first capture group is the decimal digits.

/cd=(.*)&/ - matches any string starting with an "cd=" followed by zero or more characters up to and including the first "&". The first capture group is all characters between "cd=" and "&".

They are similar, but not equivalent. Which one you should use depends on your exact needs. Judging from your comment, it sounds like you want to use:

var m = document.referrer.match(/[?&]cd=(\d+)/);
p.s.w.g
  • 136,020
  • 27
  • 262
  • 299
  • 1
    `cd=(.*)&` will allow `garbagecd=something&` to match, but it excludes the case where `cd=something` is at the end. `\&cd=([\d]*)` may not capture the full value of `cd` key, if the value contain something other than digits, and it also excludes the case where `cd` is the first key-value pair. – nhahtdh Mar 18 '13 at 17:00
  • Using the first one gives me a lot of 0 ( zero ) values - which is abnormal in this case. To clarify - I am extracting the keyword rank value from a google search results url. Apologies for not stating this earlier. – user2022284 Mar 18 '13 at 17:01
  • @user2022284 what do you mean 0 values? that it doesn't match the input string? that the capture group is empty? that the captured value is `"0"`? – p.s.w.g Mar 18 '13 at 17:07
  • The capture value is "0" in my report. Not sure if it is due to the fact that the capture group is empty, hence my question. in both cases above, what happens if the capture group is empty? – user2022284 Mar 18 '13 at 17:10
  • @user2022284 If the capture group is empty, the result will be an empty string, not `"0"`, you must be doing something (in your report) which causes an empty to evaluate to `0`. That being said, you can replace the `*` in your pattern with a `+`, to require at least one decimal in the pattern in order to match (see my update). – p.s.w.g Mar 18 '13 at 17:12
  • @p/s/w/g - thx for your suggestions. What about the issue @nhahtdh mentions above - the \&cd=([\d]*) not capturing the full value of the cd key and also not working if its the first key-value pair? – user2022284 Mar 18 '13 at 17:20
  • 1
    @user2022284 using `[?&]` will also match an ampersand or question mark preceding the `cd=`, so it will match the first appearance in url. *nhahtdh*'s point had to do with the value not being numeric. since you stated that the value *is* numeric, `\d+` should be fine for you. – p.s.w.g Mar 18 '13 at 17:58
  • thanks - testing now ... – user2022284 Mar 19 '13 at 22:05