1

I have the following strings

/search?checkin=2018-10-25&checkout=2018-10-27&id=bandung-108001534490276290&page=1&room=1&sort=popularity&type=CITY
/search?checkin=2018-12-09&checkout=2018-12-13&id=singapore-108001534490299035&maxPrice=&minPrice=&room=1&type=REGION
/search?checkin=2018-10-22&checkout=2018-10-23&lat=-6.1176043&long=106.7767146&maxPrice=&minPrice=&room=1&type=COORDINATE
/search?page=1&room=1&type=POI&id=taman-mini-indonesia-indah-110001539700828313&checkin=2018-11-14&checkout=2018-11-16&sort=distance

i want to get all string starts from &id= until the first & so they will return

id=bandung-108001534490276290
id=singapore-108001534490299035
id=taman-mini-indonesia-indah-110001539700828313

When i tried this regex \&id=.*\& it doesn't match my requirement.

Hown do i resolve this?

Cignitor
  • 891
  • 3
  • 16
  • 36

2 Answers2

1

I'd go with [?&](id=[^&]+).

  • [?&] - ? or &, because order of GET parameters is usually not guaranteed and you can get the id in the first place – something like /search?id=something-123456&checkin=2018-10-25&…
  • [^&]+ - at least one character that's not &
  • () marks a capturing group

Demo in JS:

const strings = [
  "/search?checkin=2018-10-25&checkout=2018-10-27&id=bandung-108001534490276290&page=1&room=1&sort=popularity&type=CITY",
  "/search?checkin=2018-12-09&checkout=2018-12-13&id=singapore-108001534490299035&maxPrice=&minPrice=&room=1&type=REGION",
  "/search?checkin=2018-10-22&checkout=2018-10-23&lat=-6.1176043&long=106.7767146&maxPrice=&minPrice=&room=1&type=COORDINATE",
  "/search?page=1&room=1&type=POI&id=taman-mini-indonesia-indah-110001539700828313&checkin=2018-11-14&checkout=2018-11-16&sort=distance]"
]

const regex = /[?&](id=[^&]+)/

strings.forEach(string => {
  const match = regex.exec(string)
  if (match) {
    console.log(match[1])
  }
})

Demo and explanation at Regex101: https://regex101.com/r/FBeNDN/1/

helb
  • 3,154
  • 15
  • 21
0

Positive Lookahead (?=)

Try a positive lookahead:

/&id=.+?(?=&)|&id=.+?$/gm

This part: (?=&) means: if an & is found, then everything before it is a match. The alternation:| (it's an OR logic gate) is an update in regards to a comment from Nick concerning that if the parameter ended with an &id=... It's the same match but instead of looking for a & it will look for the end of the line $. Note that the multi-line flag is used to make $ represent EOL.

Demo

var str = `/search?checkin=2018-10-25&checkout=2018-10-27&id=bandung-108001534490276290&page=1&room=1&sort=popularity&type=CITY
/search?checkin=2018-12-09&checkout=2018-12-13&id=singapore-108001534490299035&maxPrice=&minPrice=&room=1&type=REGION
/search?page=1&room=1&type=POI&id=indo-1999999051158
/search?checkin=2018-10-22&checkout=2018-10-23&lat=-6.1176043&long=106.7767146&maxPrice=&minPrice=&room=1&type=COORDINATE
/search?page=1&room=1&type=POI&id=taman-mini-indonesia-indah-110001539700828313&checkin=2018-11-14&checkout=2018-11-16&sort=distance
/search?page=1&room=1&type=POI&id=indonesia-1100055689`;
var rgx = /&id=.+?(?=&$)|&id=.+?$/gm;
var res = rgx.exec(str);

while (res != null) {
  console.log(res[0]);
  res = rgx.exec(str);
}
Community
  • 1
  • 1
zer00ne
  • 41,936
  • 6
  • 41
  • 68
  • This doesn't work if `id` is the last parameter on the line. – Nick Jan 21 '19 at 04:46
  • Indeed, I just saw that, very refined and yes it works, you were just missing the `m` flag in your answer. – zer00ne Jan 21 '19 at 05:51
  • 1
    Actually I wasn't planning to answer because the two that are here already more than cover the options. I was just playing with the code snippet that I had copied from your answer and accidentally pressed `Post Answer` instead of `Run code snippet`! :-) – Nick Jan 21 '19 at 07:05