Given a URL like this:
localhost:3000/sell-residential/house-apartment-penthouse-size-30-any-for-0-100000-in-miami%3b+orlando%3b/list-1?numRooms=2&numBaths=2&numParkingSpaces=1&misc=photo-only&interiorFeatures=heating%2Clift&exteriorFeatures=garden%2Cbalcony
I would like to extract some values using RegEx. What I have done is the following fully-commented function:
function listingTokens(reqUrl){
let retValue = {page: LISTING_PAGE_URI};
const path_parameters_matcher = /(?:([^?]+))/g;
// Group1 = url before query string
// Group2 = url after query string
let match = reqUrl.match(path_parameters_matcher);
let path = match[0];
let params = match[1];
const path_matcher = /(?:([^\/\/]+))/g;
// get the path tokens separed by /
let path_tokens = path.match(path_matcher);
let filter_values = [];
// first path token --> Mandatory gives the channel and the category
let channel_category = path_tokens[0];
// second path token --> Mandatory gives main filter values
let filters = path_tokens[1];
// third path token --> Mandatory page requested
let list_page = reqUrl.match(page_matcher)[1];
// exctract channel and category splitting the previous extracted token
let channel = channel_category.split('-')[0];
let category = channel_category.split('-').length > 1 ? channel_category.split('-')[1] : '';
// default filters object
let filtersObj = {
"channel": channel || null,
"category_type": category || null,
"property_type": [],
"localities": [],
"min_price": "any",
"max_price": "any",
"min_meter": "any",
"max_meter": "any",
"bedroom_number": "any"
};
// Regular expressions for match the filter values considering random order and possible missing path values
let property_filters_matcher = /(?=house-).*(?=-size-)|(?=house-).*(?=-for-)|(?=house-).*(?=-in-)|(?=house-).*/;
let size_filters_matcher = /(?=size-).*(?=-house-)|(?=size-).*(?=-for-)|(?=size-).*(?=-in-)|(?=size-).*/;
let price_filters_matcher = /(?=for-).*(?=-size-)|(?=for-).*(?=-house-)|(?=for-).*(?=-in-)|(?=for-).*/;
let locality_filters_matcher = /(?=in-).*(?=-size-)|(?=in-).*(?=-for-)|(?=in-).*(?=-house-)|(?=in-).*/;
// Regular expressions for match tokens separed by -
const tokens_matcher = /(?:([^-]+))/g;
// Should match for ex. house-apartment-penthouse
match = property_filters_matcher.exec(filters);
if(match){
// the first token would be the filter name and the spread result would be the values
let [filter, ...values] = match[0].match(tokens_matcher);
filtersObj.property_type = values;
}
// Should match for ex. size-30-any
match = size_filters_matcher.exec(filters);
if(match){
// the first token would be the filter name the second the from size and the third to size
let [filter, from, to] = match[0].match(tokens_matcher);
filtersObj.min_meter = from;
filtersObj.max_meter = to;
}
// Should match for ex. for-0-100000
match = price_filters_matcher.exec(filters);
if(match){
// the first token would be the filter name the second the from price and the third to price
let [filter, from, to] = match[0].match(tokens_matcher);
filtersObj.min_price = from;
filtersObj.max_price = to;
}
// Should match for ex. in-miami%3b+orlando%3b
match = locality_filters_matcher.exec(filters);
if(match){
// the first token would be the filter name the second the locality or localities
let [filter, value] = match[0].match(tokens_matcher);
// clean the values and split by separator ; (%3b)
let local = value.replace("+", "").split(';');
let localities = [];
for(let i = 0; i < local.length; i++){
// if not empty add to the result
if(local[i]){
localities.push({"key": local[i], "displayValue": local[i].replace(",", " "), "withZones": false});
}
}
filtersObj.localities = localities;
}
// Merge the objects in the final result
Object.assign(retValue, {pageNumber: list_page, selected_filters: filtersObj});
return retValue;
}
I'm not handling the possible query string values by now, but I think it would be something similar to the filters matching.
What I am asking is if there is a better way to extract those values -- I'm confident there is, because I'm pretty new in ES2015 and advanced RegExes are not my strongest field of knowledge.