如何使用Jquery拆分字符串

时间:2017-03-17 03:21:46

标签: javascript jquery regex string split

我有一个看起来像这样的字符串:

Birds(Parrort, cockatoo, Sparrow, KingFisher, Peacock), Big Dogs, Big Cats, Reptiles(Crocodile)

我想将此字符串的每个部分拆分为类似

的字符串
Birds(Parrort, cockatoo, Sparrow, KingFisher, Peacock)
Big Dogs
Big Cats
Reptiles(Crocodile)

提前致谢!

3 个答案:

答案 0 :(得分:1)

希望这会奏效。

正则表达式:[A-Za-z\s]+(?:\([A-Za-z]+(?:\s*\,\s*[a-zA-Z]+)*\))?

Regex demo

答案 1 :(得分:0)

尝试另一种方法:

将字符串更改为:

Birds(Parrort, cockatoo, Sparrow, KingFisher, Peacock)|Dogs|Cats|Reptiles(Crocodile)

使用:

var str = "Birds(Parrort, cockatoo, Sparrow, KingFisher, Peacock)|Dogs|Cats|Reptiles(Crocodile)";
var res = str.split("|");

答案 2 :(得分:0)

    var str = 'Birds(Parrort, cockatoo, Sparrow, KingFisher, Peacock),Dogs, Cats, Reptiles(Crocodile)';
    var array = str.split(", (?![^(]*\\))");
    console.log(array);