我有这种格式的字符串变量对象值:
["
traffic
Landing Hits: 0, Rewards Hits: 0
engagement
Facebook Posts: 0, Twitter Tweets: 0, Twitter Autofollows: 0, Instagram Photos: 0, Instagram Likes: 0, Instagram Votes: 0, Pinterest Pins: 0, Form Submissions: 0
conversion
Submissions: 0, Engagement: 0, Views: 0, Prints: 0
"]
我想将其转换为:
["traffic", "Landing Hits: 0", "Rewards Hits: 0"],
["engagement", "Facebook Likes: 0", "Facebook Posts: 0", "Facebook Share: 0", "Form Submissions: 0", "Instagram Likes: 0", "Instagram Photos: 0", "Instagram Votes: 0", "Pinterest Pins: 0", "Twitter Autofollows: 0", "Twitter Tweets: 0"],
["conversion", "Conversion Rate: 0", "Offers Emailed: 0", "Prints: 0", "Submissions: 0", "Views: 0"]
任何想法如何做到这一点?
答案 0 :(得分:0)
您真的不需要使用任何高级正则表达式将其拆分为数组数组。这是一个简单的(但可能不是最好的)解决方案:
var before = "\t\t\ttraffic\n\t\tLanding Hits: 0, Rewards Hits: 0\n\t\t\tengagement\n\t\tFacebook Posts: 0, Twitter Tweets: 0, Twitter Autofollows: 0, Instagram Photos: 0, Instagram Likes: 0, Instagram Votes: 0, Pinterest Pins: 0, Form Submissions: 0\n\t\t\tconversion\n\t\tSubmissions: 0, Engagement: 0, Views: 0, Prints: 0";
var pieces = before.replace(/\t/g, '').split('\n');
var group;
var result = [];
for (var i = 0; i < pieces.length; i++) {
if (i % 2) {
result.push(group.concat(pieces[i].split(/\s*,\s*/)));
} else {
group = [pieces[i]];
}
}
console.log(result);
&#13;