Cignitor
Cignitor

Reputation: 1099

Match anything before certain character

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?

Upvotes: 1

Views: 65

Answers (2)

zer00ne
zer00ne

Reputation: 43853

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);
}

Upvotes: 0

helb
helb

Reputation: 3234

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/

Upvotes: 1

Related Questions