Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/407.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 正则表达式获取匹配前后的所有文本,并在找到第二个关键字之前停止_Javascript_Regex - Fatal编程技术网

Javascript 正则表达式获取匹配前后的所有文本,并在找到第二个关键字之前停止

Javascript 正则表达式获取匹配前后的所有文本,并在找到第二个关键字之前停止,javascript,regex,Javascript,Regex,我想创建一个正则表达式,它能够抓取描述之前和之后的所有内容,直到找到下一个标题:为止 const data = `TITLE: Hitchhiker's Guide to the Galaxy AUTHOR: Douglas Adams DESCRIPTION: Seconds before the Earth is demolished to make way for a galactic freeway, Arthur Dent is plucked off the plan

我想创建一个正则表达式,它能够抓取描述之前和之后的所有内容,直到找到下一个标题:为止

  const data = `TITLE: Hitchhiker's Guide to the Galaxy
  AUTHOR: Douglas Adams
  DESCRIPTION: Seconds before the Earth is demolished to make way for a galactic freeway,
  Arthur Dent is plucked off the planet by his friend Ford Prefect, a researcher for the 
  revised edition of The Hitchhiker's Guide to the Galaxy who, for the last fifteen 
  years, has been posing as an out-of-work actor. 
  TITLE: Dune
  AUTHOR: Frank Herbert
  DESCRIPTION: The troubles begin when stewardship of Arrakis is transferred by the
  Emperor from the Harkonnen Noble House to House Atreides. The Harkonnens don't want to
  give up their privilege, though, and through sabotage and treachery they cast young 
  Duke Paul Atreides out into the planet's harsh environment to die. There he falls in 
  with the Fremen, a tribe of desert dwellers who become the basis of the army with which
  he will reclaim what's rightfully his. Paul Atreides, though, is far more than just a 
  usurped duke. He might be the end product of a very long-term genetic experiment 
  designed to breed a super human; he might be a messiah. His struggle is at the center
  of a nexus of powerful people and events, and the repercussions will be felt throughout 
  the Imperium.
  TITLE: A Song Of Ice And Fire Series
  AUTHOR: George R.R. Martin
  DESCRIPTION: As the Seven Kingdoms face a generation-long winter, the noble Stark 
  family confronts the poisonous plots of the rival Lannisters, the emergence of the 
  White Walkers, the arrival of barbarian hordes, and other threats.`
我期望的输出是

[
  "TITLE: Hitchhiker's Guide to the Galaxy
  AUTHOR: Douglas Adams
  DESCRIPTION: Seconds before the Earth is demolished to make way for a galactic freeway,
  Arthur Dent is plucked off the planet by his friend Ford Prefect, a researcher for the 
  revised edition of The Hitchhiker's Guide to the Galaxy who, for the last fifteen 
  years, has been posing as an out-of-work actor.",

  "TITLE: Dune
  AUTHOR: Frank Herbert
  DESCRIPTION: The troubles begin when stewardship of Arrakis is transferred by the
  Emperor from the Harkonnen Noble House to House Atreides. The Harkonnens don't want to
  give up their privilege, though, and through sabotage and treachery they cast young 
  Duke Paul Atreides out into the planet's harsh environment to die. There he falls in 
  with the Fremen, a tribe of desert dwellers who become the basis of the army with which
  he will reclaim what's rightfully his. Paul Atreides, though, is far more than just a 
  usurped duke. He might be the end product of a very long-term genetic experiment 
  designed to breed a super human; he might be a messiah. His struggle is at the center
  of a nexus of powerful people and events, and the repercussions will be felt throughout 
  the Imperium.",

  "TITLE: A Song Of Ice And Fire Series
  AUTHOR: George R.R. Martin
  DESCRIPTION: As the Seven Kingdoms face a generation-long winter, the noble Stark 
  family confronts the poisonous plots of the rival Lannisters, the emergence of the 
  White Walkers, the arrival of barbarian hordes, and other threats."
]

我曾尝试使用split解析字符串,但不幸的是,没有简单的字符可以拆分字符串,因为句点、逗号和换行符都存在。 我认为正则表达式是最好的选择,但我愿意接受建议,请帮助?

/?=TITLE:/g似乎是一个合理的开始。我不确定两个字符的空白是否存在于原始文本中,但将^or^添加到先行项的前面可以更好地避免误报,即/?=^TITLE:/mg、/?=^TITLE:/mg或/?=^TITLE:/mg

const data=`标题:银河系搭便车指南 作者:道格拉斯·亚当斯 描述:在地球被摧毁为银河高速公路让路前几秒钟, 阿瑟·登特是由他的朋友福特·普里菲克特(Ford Prefict)从地球上救出来的,他是该项目的研究员 《银河系世界卫生组织搭便车指南》修订版,过去15年 多年来,他一直假扮失业演员。 标题:沙丘 作者:弗兰克·赫伯特 描述:当Arrakis的管理权由 皇帝从哈科宁贵族家族到阿特雷德斯家族。哈科宁一家不想这么做 然而,放弃他们的特权,通过蓄意破坏和背叛,他们让年轻人变得更年轻 保罗·阿特雷德公爵在地球恶劣的环境中死去。他在那里跌倒了 弗里曼人是沙漠居民的一个部落,他们成为军队的基础 他将收回他应得的东西。然而,保罗·阿特雷德斯远不止是一个天才 篡位的公爵。他可能是一项长期基因实验的最终产物 旨在培育一个超级人类;他可能是个弥赛亚。他的斗争处于中心地位 强大的人物和事件之间的联系,其影响将贯穿始终 帝国。 标题:冰与火之歌系列 作者:乔治·R·马丁 描述:当七个王国面对一代人漫长的冬天时,高贵的斯塔克 这个家庭面临着竞争对手兰尼斯特的毒害阴谋,即 白人步行者、野蛮部落的到来以及其他威胁; console.logdata.split/?=标题:/g 备份计划。。。这张地图也能做到这一点

const ndata = data.split("TITLE:").map(block => "TITLE:" + block).slice(1);
const data=`标题:银河系搭便车指南 作者:道格拉斯·亚当斯 描述:在地球被摧毁为银河高速公路让路前几秒钟, 阿瑟·登特是由他的朋友福特·普里菲克特(Ford Prefict)从地球上救出来的,他是该项目的研究员 《银河系世界卫生组织搭便车指南》修订版,过去15年 多年来,他一直假扮失业演员。 标题:沙丘 作者:弗兰克·赫伯特 描述:当Arrakis的管理权由 皇帝从哈科宁贵族家族到阿特雷德斯家族。哈科宁一家不想这么做 然而,放弃他们的特权,通过蓄意破坏和背叛,他们让年轻人变得更年轻 保罗·阿特雷德公爵在地球恶劣的环境中死去。他在那里跌倒了 弗里曼人是沙漠居民的一个部落,他们成为军队的基础 他将收回他应得的东西。然而,保罗·阿特雷德斯远不止是一个天才 篡位的公爵。他可能是一项长期基因实验的最终产物 旨在培育一个超级人类;他可能是个弥赛亚。他的斗争处于中心地位 强大的人物和事件之间的联系,其影响将贯穿始终 帝国。 标题:冰与火之歌系列 作者:乔治·R·马丁 描述:当七个王国面对一代人漫长的冬天时,高贵的斯塔克 这个家庭面临着竞争对手兰尼斯特的毒害阴谋,即 白人步行者、野蛮部落的到来以及其他威胁` const ndata=data.splitTITLE:.mapblock=>TITLE:+block.slice1; console.lognda您可以使用:

data.split/?=标题:/g; 如果不需要尾随空格,则可以在拆分模式中删除它:

data.split/\s*?=标题:/g;
也许/^\s*?=TITLE:/mg?是的,我忘了带m旗。这太棒了,非常感谢!我喜欢regexp,但像这样的其他方法通常更容易编写和维护。即使OP有一个好的RegExp,这也可能是我会选择的解决方案。这相当狡猾!非正则表达式解决方案无法轻松完成行的开头之类的操作,因此精度较低,并可能产生误报。当Regex被误用时,它的名声不好,但这是一个很好的用例,因为有一个特定的、直接的、干净的解决方案,易于编写、读取和维护。使用这种方法,如果TITLE:没有启动整个字符串,那么最后将导致slice1截断相关信息,因此它不像所有正则表达式坏/令人惊讶、所有非正则表达式好/不令人惊讶那么简单。使用sli
ce1基本上是为了掩盖出现的空字符串。@ggorlen-事实上,这是一种黑客行为,不过如果代码中多了几个字符,情况可能就不那么糟了。我同意regex是一个更优雅的解决方案-这实际上是为了展示另一种解析数据的方式,而不是对OP regex特定问题的直接回答。是的,这根本不是一个糟糕的答案,只是提到这一点,主要是回应Tom,当适当使用regex时,也许我应该特别使用@Theme。。。。