const regex = /https?:\/\/(?:[^\/]+\/){4}(.*)/g;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('https?:\\\/\\\/(?:[^\\\/]+\\\/){4}(.*)', 'g')
const str = `https://firstURL/Second/Third/Fourth/five/SearchResults?json=%7B%22SalesInventoryID%22%3Anull%2C%22BundleInventoryID%22%3Anull%2C%22InventorySalesPriceID%22%3Anull%2C%22SiteType%22%3A8%2C%22SiteName%22%3A%22Desert%20Club%22%2C%22PointsMin%22%3A1%2C%22PointsMax%22%3A999999%2C%22RoomType%22%3Anull%2C%22PriceMin%22%3A1%2C%22PriceMax%22%3A999999%2C%22SeasonType%22%3Anull%2C%22FrequencyType%22%3Anull%2C%22CheckInType%22%3Anull%2C%22WeekMin%22%3A1%2C%22WeekMax%22%3A52%2C%22RoomMin%22%3A%221%22%2C%22RoomMax%22%3A%22999999%22%2C%22TourID%22%3A2022154%2C%22TourLocationID%22%3A54%2C%22ListResults%22%3Anull%2C%22BundledResults%22%3Anull%2C%22TourParticipantInfo%22%3A%7B%22TourID%22%3A2022154%2C%22SiteID%22%3A1%2C%22SiteName%22%3Anull%2C%22PersonID%22%3A364182%2C%22FirstName%22%3A%22Lynn%20A.%22%2C%22LastName%22%3A%22McDougald%22%2C%22City%22%3A%22Charlotte%22%2C%22State%22%3A%22-%22%2C%22ShortState%22%3A%22-%22%2C%22Country%22%3Anull%2C%22CountryID%22%3A840%2C%22OwnerNumber%22%3A%226017104%22%2C%22OwnerStatus%22%3Anull%2C`;
// Reset `lastIndex` if this regex is defined globally
// regex.lastIndex = 0;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for JavaScript, please visit: https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions