我有一个看起来像这样的字符串:
var str = "'1vK3KfqgSHqv5Y0066FnIY',#41,$,$,(#149,#488,#862,#945,#1028,#1249),#114";
通常我只会:
str.split(",");
但这将导致:
[
"'1vK3KfqgSHqv5Y0066FnIY'",
"#41",
"$",
"$",
"(#149",
"#488",
"#862",
"#945",
"#1028",
"#1249)",
"#114"
]
而所需的结果是:
[
"'1vK3KfqgSHqv5Y0066FnIY'",
"#41",
"$",
"$",
"(#149, #488, #862, #945, #1028, #1249)",
"#114"
]
我将如何实现?
最佳答案
嗨,这里是一个正则表达式
str.match(/(\(.*?\)|[^\(,\s]+)(?=\s*,|\s*$)/g)
导致
[ '\'1vK3KfqgSHqv5Y0066FnIY\'',
'#41',
'$',
'$',
'(#149,#488,#862,#945,#1028,#1249)',
'#114' ]