将逗号分隔的字符串转换为嵌套数组,RegExp?

时间:2023-01-19 00:17:29

Got this type of string:

得到这种类型的字符串:

var myString = '23, 13, (#752, #141), $, ASD, (#113, #146)';

I need to split it to an array with comma as separator but also converts (..) to an array.

我需要将它分割为一个具有逗号分隔符的数组,但也需要将(..)转换为数组。

This is the result I want: [23, 13, ['#752', '#141'], '$', 'ASD', ['#113', '#146']];

这是我想要的结果:[23日13日[# 752,# 141的),“美元”,“自闭症”,[# 113,# 146的]];

I got huge data-sets so its very important to make it as fast as possible. What's the fastest way? Do some trick RegExp function or do it manually with finding indexes etc.?

我有大量的数据集,所以要尽可能快的完成它非常重要。最快的方法是什么?做一些有技巧的RegExp函数或者手工查找索引?

Here's a jsbin: https://jsbin.com/cilakewecu/edit?js,console

这里有一个jsbin:https://jsbin.com/cilakewecu/edit?js,控制台

3 个解决方案

#1


5  

Convert the parens to brackets, quote the strings, then use JSON.parse:

将括号转换为括号,引用字符串,然后使用JSON.parse:

JSON.parse('[' + 
  str.
    replace(/\(/g, '[').
    replace(/\)/g, ']').
    replace(/#\d+|\w+/g, function(m) { return isNaN(m) ? '"' + m + '"' : m; })
  + ']')

> [23,13,["#752","#141"],"ASD",["#113","#146"]]

#2


2  

You can use RegEx

您可以使用正则表达式

/\(([^()]+)\)|([^,()\s]+)/g

RegEx Explanation:

正则表达式的解释:

The RegEx contain two parts. First, to capture anything that is inside the parenthesis. Second, capture simple values (string, numbers)

RegEx包含两个部分。首先,捕捉括号内的任何内容。其次,捕获简单的值(字符串、数字)

  1. \(([^()]+)\): Match anything that is inside the parenthesis.
    • \(: Match ( literal.
    • \(:匹配(文字。
    • ([^()]+): Match anything except ( and ) one or more number of times and add the matches in the first captured group.
    • ([^()]+):除了(和)一个或多个匹配的次数并添加第一个捕获组的比赛。
    • \): Match ) literal.
    • 文字\):匹配)。
  2. \(((^()]+)\):匹配任何括号内。\(:匹配(文字。([^()]+):除了(和)一个或多个匹配的次数并添加第一个捕获组的比赛。文字\):匹配)。
  3. |: OR condition in RegEx
  4. 或RegEx中的条件
  5. ([^,()\s]+): Match any character except , (comma), parenthesis ( and ) and space one or more number of times and add the match in the second captured group
  6. ([^,()\]+):匹配任何字符除外,(逗号),括号()和空间的次数并添加一个或多个匹配在第二组

将逗号分隔的字符串转换为嵌套数组,RegExp?

Demo:

演示:

var myString = '23, 13, (#752, #141), ASD, (#113, #146)',
    arr = [],
    regex = /\(([^()]+)\)|([^,()\s]+)/g;

// While the string satisfies regex
while(match = regex.exec(myString)) {

    // Check if the match is parenthesised string
    // then
    //     split the string inside those parenthesis by comma and push it in array
    // otherwise
    //     simply add the string in the array
    arr.push(match[1] ? match[1].split(/\s*,\s*/) : match[2]);
}

console.log(arr);
document.body.innerHTML = '<pre>' + JSON.stringify(arr, 0, 4) + '</pre>'; // For demo purpose only

#3


-2  

Just use the split method.

使用分割方法。

var str = '23, 13, (#752, #141), ASD, (#113, #146)',
    newstr = str.replace(/\(/gi,'[').replace(/\)/gi,']'),
    splitstr = newstr.split(',');

#1


5  

Convert the parens to brackets, quote the strings, then use JSON.parse:

将括号转换为括号,引用字符串,然后使用JSON.parse:

JSON.parse('[' + 
  str.
    replace(/\(/g, '[').
    replace(/\)/g, ']').
    replace(/#\d+|\w+/g, function(m) { return isNaN(m) ? '"' + m + '"' : m; })
  + ']')

> [23,13,["#752","#141"],"ASD",["#113","#146"]]

#2


2  

You can use RegEx

您可以使用正则表达式

/\(([^()]+)\)|([^,()\s]+)/g

RegEx Explanation:

正则表达式的解释:

The RegEx contain two parts. First, to capture anything that is inside the parenthesis. Second, capture simple values (string, numbers)

RegEx包含两个部分。首先,捕捉括号内的任何内容。其次,捕获简单的值(字符串、数字)

  1. \(([^()]+)\): Match anything that is inside the parenthesis.
    • \(: Match ( literal.
    • \(:匹配(文字。
    • ([^()]+): Match anything except ( and ) one or more number of times and add the matches in the first captured group.
    • ([^()]+):除了(和)一个或多个匹配的次数并添加第一个捕获组的比赛。
    • \): Match ) literal.
    • 文字\):匹配)。
  2. \(((^()]+)\):匹配任何括号内。\(:匹配(文字。([^()]+):除了(和)一个或多个匹配的次数并添加第一个捕获组的比赛。文字\):匹配)。
  3. |: OR condition in RegEx
  4. 或RegEx中的条件
  5. ([^,()\s]+): Match any character except , (comma), parenthesis ( and ) and space one or more number of times and add the match in the second captured group
  6. ([^,()\]+):匹配任何字符除外,(逗号),括号()和空间的次数并添加一个或多个匹配在第二组

将逗号分隔的字符串转换为嵌套数组,RegExp?

Demo:

演示:

var myString = '23, 13, (#752, #141), ASD, (#113, #146)',
    arr = [],
    regex = /\(([^()]+)\)|([^,()\s]+)/g;

// While the string satisfies regex
while(match = regex.exec(myString)) {

    // Check if the match is parenthesised string
    // then
    //     split the string inside those parenthesis by comma and push it in array
    // otherwise
    //     simply add the string in the array
    arr.push(match[1] ? match[1].split(/\s*,\s*/) : match[2]);
}

console.log(arr);
document.body.innerHTML = '<pre>' + JSON.stringify(arr, 0, 4) + '</pre>'; // For demo purpose only

#3


-2  

Just use the split method.

使用分割方法。

var str = '23, 13, (#752, #141), ASD, (#113, #146)',
    newstr = str.replace(/\(/gi,'[').replace(/\)/gi,']'),
    splitstr = newstr.split(',');