拆分和融合词

编程入门 行业动态 更新时间:2024-10-10 01:17:41
本文介绍了拆分和融合词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

实际上,我有这段代码将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);

更多推荐

拆分和融合词

本文发布于:2023-11-23 06:38:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1620461.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!