字符串解析并创建新的搜索数组

时间:2018-10-31 10:29:44

标签: javascript arrays regex reactjs parsing

我从后端得到一个这样的字符串(即使用<pre></pre>来格式化后端数据库中的方式)

Main responsibilities will include:
 - Driving creation of customer attractive brand
 - Making a significant impact on lead generation and quality of the incoming pipeline
 - Creating marketing materials (Company overview, Company presentation, Case studies, Brochure, Video)
 - Online Advertisement

我正在尝试解析此字符串,并将所有- something (以'-'开头的字符串)放入这些项的一个数组中。所以我需要

let arr = ["Driving creation of customer attractive brand", "Making a significant impact on lead generation and quality of the incoming pipeline", "...", "Online Advertisement"]

任何想法如何实现?

1 个答案:

答案 0 :(得分:1)

您可以按照以下示例进行操作:

const str = `Main responsibilities will include:
 - Driving creation of customer attractive brand
 - Making a significant impact on lead generation and quality of the incoming pipeline
 - Creating marketing materials (Company overview, Company presentation, Case studies, Brochure, Video)
 - Online Advertisement`;

let regex = /(?<=^\s-\s).*/gm;
let match = str.match(regex);
console.log(match);

regex101上的详细信息:https://regex101.com/r/xdyCzO/1
一些注意事项:
(?<=^\s-\s) =正向后看,它在每个句子的-开始之后得到所有东西(\ s = 1个空格,^ =行的开头)
.* =除换行符以外的任何字符,*表示零或更多