首页 > 解决方案 > 正则表达式过滤数组并保留特定部分

问题描述

目前,我正在尝试找出输入为 的正则表达式系统["title of the poll, polloption1 polloption2, polloption3 polloption4"]

我怎样才能使 RegX 过滤掉任何东西并返回给我['polloption1 polloption2', 'polloption3 polloption4']

我当前的代码:

const s = ["title of the poll, polloption1 polloption2, polloption3 polloption4"];
console.log(s.split(/(?:^|")([^\s"]+)(?:\s+[^\s"]+)*/).filter(Boolean));
//returns: [ '+poll', ' ', 'option1', '" ', 'option2', '"' ] which is very inacurate

标签: javascriptregex

解决方案


您可以使用

s[0].split(/\s*,\s*/).slice(1)

它产生[ "polloption1 polloption2", "polloption3 polloption4" ].

/\s*,\s*/则表达式匹配用零个或多个空格括起来的任何逗号。从结果.slice(1)数组中删除第一项。

请参阅正则表达式演示

请参阅 JavaScript 演示:

const s = ["title of the poll, polloption1 polloption2, polloption3 polloption4"];
console.log(s[0].split(/\s*,\s*/).slice(1));


推荐阅读