简体   繁体   English

按正则表达式模式 javascript 对多行字符串进行分组

[英]Group multiline string by regex pattern javascript

I have a multiline string that I want to split and group by a certain regex pattern that appears several times throughout the string我有一个多行字符串,我想通过在整个字符串中出现多次的某个正则表达式模式对其进行拆分和分组

Some filler
at the beginning
of the text

Checking against foo...
Some text here
More text
etc.

Checking against bar...
More text
moremoremore

Using the above, I'd like to group by the value following the term Checking against (so in this example foo and bar , and in those groups would be the text following that line, up until the next occurrence使用上述内容,我想按术语Checking against之后的值进行分组(因此在本例中foobar ,并且在这些组中将是该行之后的文本,直到下一次出现

So the resulting output would be something like the below, allowing access to the values by the grouping name因此生成的 output 将如下所示,允许通过分组名称访问值

{
  foo: 'Some text here\nMore text\netc.'
  bar: 'More text\nmoremoremore'
}

My initial approach was to split the string on the newlines into an array of elements, I'm then struggling to我最初的方法是将换行符上的字符串拆分为一个元素数组,然后我正在努力

  • Find occurrence of "Checking against" and set that as the key查找“Checking against”的出现并将其设置为键
  • Append every line up until the next occurrence as the value Append 每行直到下一次出现作为值

maybe you can try this也许你可以试试这个

 const str = `Some filler at the beginning of the text Checking against foo... Some text here More text etc. Checking against bar... More text moremoremore`; let current = null; const result = {}; str.split("\n").forEach(line => { const match =line.match(/Checking against (.+?)\.\.\./); if (match) { current = match[1]; } else if (current && line;== "") { if (result[current]) { result[current] += "\n" + line } else { result[current] = line; } } }). console.log(result)

There are many ways to do that.有很多方法可以做到这一点。 You could use split to split the whole text by "Checking against" and at the same time capture the word that follows it as part of the splitting separator.您可以使用split通过“检查”来拆分整个文本,同时捕获其后面的单词作为拆分分隔符的一部分。

Then ignore the intro with slice(1) , and transform the array of keyword, text parts into an array of pairs, which in turn can be fed into Object.fromEntries .然后忽略slice(1)的介绍,并将关键字数组、文本部分转换为对数组,然后可以将其输入Object.fromEntries That will return the desired object:这将返回所需的 object:

 let data = `Some filler at the beginning of the text Checking against foo... Some text here More text etc. Checking against bar... More text moremoremore`; let result = Object.fromEntries( data.split(/^Checking against (\w+).*$/gm).slice(1).reduce((acc, s, i, arr) => i%2? acc.concat([[arr[i-1], s.trim()]]): acc, []) ); console.log(result);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否不可能在 JavaScript 中使用多行正则表达式模式指示输入字符串的开头? - Is it impossible to indicate the start of the input string using a multiline regex pattern in JavaScript? 使用 RegEx 和 javascript 剪切多行字符串 - Cut multiline string with RegEx and javascript JavaScript Regex-通过Regex模式将字符串拆分为数组 - JavaScript Regex - Splitting a string into an array by the Regex pattern Javascript正则表达式多组匹配模式 - Javascript Regex multiple group matches in pattern Javascript多行正则表达式先行或其余字符串/字符串结尾 - Javascript multiline regex lookahead OR the rest of string / very end of string 在 javascript 的分隔多行字符串中查找单词的正则表达式模式 - Regular expression pattern to find a word in the delimited multiline string in javascript Javascript Regex多行无法正常工作 - Javascript Regex multiline not working JavaScript正则表达式测试多行字符串是否以回车结尾 - JavaScript regex to test if multiline string ends with a carriage return javascript正则表达式,用多行字符串中的单个实例替换多个实例 - javascript regex, replace multiple instances with a single one in multiline string 正则表达式对字符串中的递归模式进行分组并将捕获组作为数组返回 - Regex to group recursive pattern in string and return capture group as array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM