我有一个像这样的字符串数组。
var items= ['products/600/application/286/tabbed/301/text',
'products/600/application/286/tabbed/305/text',
'products/600/application/500/tabbed/200/text',
'products/600/application/500/tabbed/300/text',
'products/600/application/286/tinytext',
'products/600/differenttext'];
我需要用连续数字替换数字(相同的数字应该相同),输出应该如下所示:
var items= ['products[1]/application[1]/tabbed[1]/text',
'products[1]/application[1]/tabbed[2]/text',
'products[1]/application[2]/tabbed[1]/text',
'products[1]/application[2]/tabbed[2]/text',
'products[1]/application[1]/tinytext',
'products[1]/differenttext'];
请注意,相同的数字应该得到相同的结果,例如,所有286应该是[1]因为它是最低的而500应该被[2]替换,因为它是垂直中的下一个更大的数字。把它们想象成孩子。例如,对于行
products/600/application/286/tabbed/301/text,
products/600/application/286/tabbed/305/text,
直到tabbed
,但在此之后,有2个不同的孩子,第一个人的身份是301秒是305.所以他们应该[1]和[2]。现在看一下行
products/600/application/286/tabbed/305/text,
products/600/application/500/tabbed/200/text,
products/600/application/500/tabbed/300/text,
在这种情况下,应用程序有两个孩子。 286和500.第二个是500,有2个孩子。所以500的孩子应该是1和2(200和300)
申请temp= items[0].match(/\d+/g);
后,我得到了结果[600,286,301]
。我应该使用json对象来放置temp
得到的所有结果,还是使用2d数组?
答案 0 :(得分:3)
var items = ['products/600/application/286/tabbed/301/text',
'products/600/application/286/tabbed/305/text',
'products/600/application/500/tabbed/200/text',
'products/600/application/500/tabbed/300/text',
'products/600/application/286/tinytext',
'products/600/differenttext'
];
var num = {};
var res = items.map(function(v, i) { // iterate over the array for generating updated array
return v.replace(/\/(\d+)\/(?:(.*?)\/(\d+)\/(?:(.*?)\/(\d+)\/)?)?/, function(m, m1, m2, m3, m4, m5) { //find the number to replace
var str = replace(m1, 'main', 0);
if (m3) {
str += m2 + replace(m3, m1);
if (m5)
str += m4 + replace(m5, m1 + '_' + m3);
}
return str;
});
});
function replace(mat, ind1) {
if (!num[ind1])
num[ind1] = {
ind: 0
};
if (num[ind1][mat]) // check index already defined
return '/[' + num[ind1][mat] + ']/'; // if defined use the previous
num[ind1][mat] = ++num[ind1].ind ;// else update index in object
return '/[' + num[ind1].ind + ']/'; // and return the replace string
}
document.write('<pre>' + JSON.stringify(res, null, 3) + '</pre>');
更新: 如果出现次数超过3,那么你可以这样做,
var items = ['products/600/application/286/tabbed/301/text/222/kjkjk',
'products/600/application/286/tabbed/301/text/112/kjkjk',
'products/600/application/286/tabbed/305/text/222/kjkjk',
'products/600/application/500/tabbed/200//text/222/kjkjk',
'products/600/application/500/tabbed/300/text',
'products/600/application/286/tinytext',
'products/600/differenttext'
];
var num = {};
var res = items.map(function(v, i) { // iterate over the array for generating updated array
var match = 'match';
return v.replace(/\/(\d+)\//g, function(m, m1) {
var res = replace(match, m1);
match += '_' + m1;
return res;
});
});
78
function replace(ind1, m1) {
if (!num[ind1])
num[ind1] = {
ind: 0
};
if (!num[ind1][m1])
num[ind1][m1] = ++num[ind1].ind;
return '/[' + num[ind1][m1] + ']/';
}
document.write('<pre>' + JSON.stringify(res, null, 3) + '</pre>');