0

How can I use regex and the replace function to retrieve:

"?topic=MyCategory"

From these strings:

"?topic=marketing&page=1"
"?topic=Sales&page=2"
"?topic=Recruiting"
"?page=2&topic=recruiting"

But retrieves null or an empty string from these:

"?page=4"
"" (empty string)

I've been able to get to: \?topic=(.*?)& and replace with $1.

And hope it's ok to ask as a follow up question, where can I find a good regex guide/cheatsheet.

ddy250
  • 211
  • 3
  • 16
  • You should replace `(.*?)&` with `[^&]*` or `[^&]+`. And use `String#match` or `RegExp#exec` to extract the substring. [Here is an awesome](https://stackoverflow.com/questions/22937618/reference-what-does-this-regex-mean) regex knowledge source. – Wiktor Stribiżew Dec 04 '18 at 12:47
  • I think you should use: [^&"]+ to get category when there's one or two pairs. – Poul Bak Dec 04 '18 at 12:52

0 Answers0