2

All:

I am pretty new to Regex in JS, if I have a string like( of course not this simple, but this part is most important to me):

?view=Peer%20Comparison&&userid=123

I want to extract the "view=Peer%20Comparison" part with non-capture group and capture group(there may be multiple query parameter pairs)

Could anyone help me with this(I guess this is very simple to regex guru, but I just can not think out of any, sorry for interrupt)?

Thanks

Kuan
  • 11,149
  • 23
  • 93
  • 201
  • what do you mean by this `capture group(there may be multiple query parameter pairs)`? could you post a complex example? – Avinash Raj Feb 12 '15 at 17:38
  • You can use `split` and pass `?` as separator.. – Rakesh_Kumar Feb 12 '15 at 17:39
  • @Kuan: is there a particular reason why you want to omit capture groups? – Willem Van Onsem Feb 12 '15 at 17:42
  • @AvinashRaj What I want to do is extract the url query parameter pairs from url with non-capture and capture group skill. – Kuan Feb 12 '15 at 17:42
  • @Rakesh_Kumar What I want to do is extract the url query parameter pairs from url, and I want to learn how to use non-capture and capture group and apply them to the url to get query prameter key value pair. – Kuan Feb 12 '15 at 17:44
  • @CommuSoft the parts I want to omit is that ? mark and && which seperate key-value pairs – Kuan Feb 12 '15 at 17:45
  • Looks like you're trying to parse query parameters. Here's an SO answer for that question: http://stackoverflow.com/a/901144/2482570 – Max Heiber Feb 12 '15 at 17:49
  • @mheiber Thanks, that definitely work, and I can also just simply use split if I really want to get job done, but I just want to learn how to use non-capture group to get the starting position after ? mark and use capture group to extract each key value pair. – Kuan Feb 12 '15 at 17:56
  • you cannot get anything from a non-capture group. they are non-capturing. they are only used for grouping. – akonsu Feb 12 '15 at 18:10
  • @akonsu That is exactly what I want. I do not want the ? mark, I only need to get the position which follow it(I just want to indicate ? as a position pattern and get what following it. I read some posts, it is called lookahead or something like that) – Kuan Feb 12 '15 at 23:38

1 Answers1

4

Answering the OP question for the sake of knowledge, even known a solution was found.

The syntax for non capturing group in JavaScript regular expression is:

(?:pattern)

as stated in https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/RegExp#grouping-back-references

Also you should take a look at the lookahead assertions (e.g. x(?=y) and x(?!y)) in the same page as it may help.

Pedro Sanção
  • 1,328
  • 1
  • 11
  • 16