【发布时间】:2021-05-14 17:25:13
【问题描述】:
如果该段落包含使用 JS 的关键搜索词,我正在尝试从 OCR 合同中提取段落。用户可能会搜索诸如“提前发货”之类的内容来查找与某个客户的订单是否可以提前发货相关的条款。
我已经把头撞在正则表达式墙上很长一段时间了,显然我没有抓住什么。
如果我有这样的文字并且我正在搜索“匹配”这个词:
let text = "\n\nThis is an example of a paragraph that has the word I'm looking for The word is Match. \n\nThis paragraph does not have the word I want."
我想提取双 \n 字符之间的所有文本,而不是返回该字符串中的第二个句子。
我一直在尝试某种形式:
let string = `[^\n\n]*match[^.]*\n\n`;
let re = new RegExp(string, "gi");
let body = text.match(re);
但是,它返回 null。奇怪的是,如果我从它工作的字符串中删除句点(排序):
[
"This is an example of a paragraph that has the word I'm looking for The word is Match \n" +
'\n'
]
任何帮助都会很棒。
【问题讨论】:
-
试试这个:
[^\n].*match.*[^\n]。编辑:我猜这个?可能是?\n{2,}(.*match.*)\n{2,}
标签: javascript node.js regex regex-lookarounds regex-group