拆分和融合词 [英] Split and Fuse words
问题描述
实际上,我有这段代码将Obj内的Word接收起来,并且为每列单独创建了一个新行(这实际上工作得很好),而这正是我想要的.但是我注意到,在某些情况下,我在同一组单词中用逗号分隔.
Actually, I have this piece of code that takes the Words inside the Obj and it separates creating a new row for each column (this actually works perfectly), and it's exactly what I want. But I've noticed on some cases I have in the same group words separated by a comma.
示例:马萨乔,桑德罗·波蒂切利,莱昂纳多·达·芬奇"我们在这里有3个字.该代码从这3个单词生成新行.
Example: "Masaccio, Sandro Botticelli, Leonardo da Vinci" We have here 3 words. The code generates a new row from those 3 words.
但是当我有这个时会发生什么?马萨乔,桑德罗·波提切利,莱昂纳多,达,芬奇"脚本认为那里有5个字.所以我的问题是:我应该如何创建这样的东西:马萨乔,桑德罗·波提切利,莱昂纳多,达,芬奇"使用此引号"将其视为一个单词而不是3个单词.
But what happens when I have this? "Masaccio, Sandro Botticelli, Leonardo, da, Vinci" The script thinks there we have 5 words. So my question is: how I should create something like this: "Masaccio, Sandro Botticelli, 'Leonardo, da, Vinci'" Using this 'quotes' to consider it as only one word instead of 3.
有什么想法吗?谢谢!
const Obj = {
"0":"Masaccio, Sandro Botticelli, Leonardo da Vinci",
"1":"Random, Thing, Uploaded",
"2":"String, Second String, Third string",
"3":"Chef, Police, Cat",
"4":"Legen, Jerry, Jack",
};
const Obj3 = [];
var count = Obj[0].split(", ").length;
var countOuter = Object.keys(Obj).length;
for( var i = 0; i < count; i++){
var string = [];
for( var j = 0; j < countOuter; j++){
string.push(Obj[j].split(", ")[i]);
}
Obj3[i] = string;
}
console.log(Obj3);
推荐答案
一种可能性是创建一个正则表达式来匹配逗号分隔的单词('
允许),或匹配开始的'
报价,直到匹配的结束的'
:
One possibility would be to create a regular expression to match
comma-separated words (no quotes '
allowed), OR match a starting '
quote up until its matching ending '
:
const Obj = {
"0":"Masaccio, Sandro Botticelli, 'Leonardo, da, Vinci'",
"1":"Random, Thing, Uploaded",
"2":"String, Second String, Third string",
"3":"Chef, Police, Cat",
"4":"Legen, Jerry, Jack",
};
const result = Object.values(Obj).reduce((a, str) => {
const items = str.match(/\w[\w\s]+(?=, |$)|'[^']+'/g);
items.forEach((item, i) => {
if (!a[i]) a[i] = [];
a[i].push(item);
});
return a;
}, []);
console.log(result);
这篇关于拆分和融合词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!