param1/param2/param3
param1/param2
- это не param1
в который входит неэкранированный слэш?string path = "param1/param2/name1=value1&name2=value2";
pos = 0;
int posEnd = path.length();
int mode = 1;
int index = 1;
string name = "", value = "";
while (true) {
if (pos >= posEnd || path[pos] == '/' || path[pos] == '&') {
if (mode == 1) {
if (name != "") {
value = name;
name = "p" + to_string(index);
index++;
params.insert(name, value);
name = "";
value = "";
}
}
else {
params.insert(name, value);
name = "";
value = "";
mode = 1;
}
if (pos >= posEnd) break;
}
else if (path[pos] == '=') {
mode = 2;
}
else {
if (mode == 1) name += path[pos]; else value += path[pos];
}
pos++;
}
function decodePath(url){
var parts=(url+"").split("?",2),
path=(parts[0]+'').split("/"),
query=(parts[1]+'').split("&"),
params={};
query.forEach(function(value){
var group=value.split("=");
params[group[0]]=group[1];
});
return {
path:path,
params:params
};
}
console.log(decodePath("param1/param2/?name1=value1&name2=value2"));
/*
Вернет
params: Object { name1="value1", name2="value2"}
path : ["param1", "param2", ""]
*/