简体   繁体   English

拆分但要尊重逗号名称

[英]Split but respect comma names

I'm having a little trouble with a code. 我在编写代码时遇到了一些麻烦。 I have a list of words that need to be separated and parsed instead of rows in columns. 我有一个单词列表需要分隔和解析,而不是列中的行。

This code actually works very well and does that job. 这段代码实际上很好地工作了。 How is actually working: 实际运作方式:

 const Obj = { "0":"Mario, Louigi, Peach", "1":"Wario, King Kong, Bomberman", "2":"Dracula, Doctor Z, Raikou" }; 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); 

But if any name has a comma in the middle example: Mr, Robot it considers that comma as a separator comma and divides Mr, Robot in two instead of one. 但是,如果在中间的示例中有一个名称用逗号表示:Robot先生,它将逗号视为分隔符,并将Robot先生一分为二而不是一。

Example: 例:

  const Obj = { "0":"Mr, Robot, Louigi, Peach", "1":"Wario, King Kong, Bomberman", "2":"Dracula, Doctor Z, Raikou" }; 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); 

And then it creates an undefined record that causing me errors. 然后,它创建了一个未定义的记录,导致我出错。 Any idea how to solve this? 任何想法如何解决这个问题? Thanks in advance!! 提前致谢!!

If you can't get the names before they've been joined by commas, you might be able to use a regex delimiter to split them. 如果在用逗号将名称连起来之前无法获得名称,则可以使用正则表达式定界符来分割它们。 For example, if it would suffice to not split at commas preceded by only two non-whitespace characters, you could use: 例如,如果只用两个非空白字符隔开逗号是足够的,则可以使用:

.split(/(?<=[^ ,]{3,}), /)

This regex will match ', ' only if it is preceded by at least 3 characters that don't match ' ' or ',' . 该正则表达式将匹配', ' ,前提是它前面必须有至少三个不匹配' '','字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM