I have this string :
var data = "res_per_page=10&page_num=1&location_id=107&location_id=174&location_id=110&location_id=180"
I'd like to group all 'location_id' parameters into one, separated by _
. How to achieve that ? The re开发者_开发问答sult string should be as follows :
var data = "res_per_page=10&page_num=1&location_id=107_174_110_180"
How about; (assumes id's are numeric from 1 to 9 digits)
var newdata = [];
data = data.replace(/&?location_id=(\d{1,9})/ig, function(m, k, v) {
newdata.push(k);
return "";
});
data += "&location_id=" + newdata.join("_");
alert(data);
in: "res_per_page=10&page_num=1&location_id=107&location_id=174&location_id=110&location_id=180"
out: "res_per_page=10&page_num=1&location_id=107_174_110_180"
Here you go:
var dataInit = "res_per_page=10&page_num=1&location_id=107&"+
"location_id=174&location_id=110&location_id=180"
.split('&location_id='),
data = dataInit[0]+'&location_id='+dataInit.slice(1).join('_');
Now data
's value is: res_per_page=10&page_num=1&location_id=107_174_110_180
精彩评论