How to split string while ignoring portion in parentheses?

前端 未结 5 1485
南方客
南方客 2020-12-11 17:51

I have a string that I want to split into an array by using the commas as delimiters. I do not want the portions of the string that is between parentheses to be split thoug

5条回答
  •  余生分开走
    2020-12-11 18:28

    var start = "bibendum, morbi, non, quam (nec, dui, luctus), rutrum, nulla";
    start = start.replace(/ /g,'');
    console.log(start);
    
    var front = start.substring(0,start.lastIndexOf('(')).split(',');
    var middle = '('+start.substring(start.lastIndexOf('(')+1,start.lastIndexOf(')'))+')';
    var end = start.substring(start.lastIndexOf(')')+2,start.length).split(',');
    console.log(front)
    console.log(middle)
    console.log(end)
    return front.concat(middle,end);
    

提交回复
热议问题