javascript - 如何提取正则表达式中由单词后跟分号分隔的句子?

标签 javascript regex

我正在尝试编写一些正则表达式模式以从一段文本中删除位置、功能和付款接受部分。我正在制作一个显示食物选项的网站,在 API 中,它们有一个描述部分,其中包含的信息绰绰有余,这就是我想要提取特定文本的原因。

我研究了正则表达式中的正面和负面前瞻,但我仍然无法解决我的问题。我可以选择第二部分之前的所有内容,但前提是在这种情况下我选择了位置。如果我选择功能,我也会选择上一节,位置。请参阅以下文本作为示例。

这是我要从中提取的文本:

Location: Village 1 \r\n\r\nFeatures:  A multitude of offerings, including entrees, hot meals, wood-fired pizza, salad bar, grill items, made-to-order deli sandwiches & wraps, convenience items and much more\r\n\r\nPayment accepted: cash, Watcard  \r\n\r\nThis is a great place to meet your friends! The aroma of fresh baked breads and pastries from our in-house UW Bakery will surely make you take a deep breath. Mudie’s offers a large selection of vegetarian foods, grab n’ go items, salad bar, grill items, made-to-order deli sandwiches and pitas, full breakfast, and convenience foods. A hot entrée item and side dishes are available every lunch and dinner hour.\r\n\r\nMeal hours for Mom's Counter*:\r\n\r\nBreakfast: 7:30 - 11:00 am\r\n\r\nLunch:11:30 am - 2:00 pm\r\n\r\nDinner: 4:30 - 8:00 pm \r\n\r\n*please note, these hours are subject to change without notice "

到目前为止我是这样写的:

  /.+?(?=Payment accepted)/

它会选择付款已接受部分之前的所有内容。我也写了

/(Location|Features|Payment accepted):\s{1,4}?[A-Z]+\s?\d?/

它选择了我想要的三个地方的部分。我无法将两者联系起来,也无法想出任何能够在不包含其他部分的情况下选择我需要的内容的方法。 任何帮助将不胜感激。

所以在上面的例子中,我提取的部分是:

Location: Village 1
Features:  A multitude of offerings, including entrees, hot meals, wood-fired pizza, salad bar, grill items, made-to-order deli sandwiches & wraps, convenience items and much more
Payment accepted: cash, Watcard

最佳答案

您可以使用此正则表达式来提取这三部分文本,

/Location:\s*([^\v]*)\s*Features:\s*([^\v]*)Payment accepted:(.*?)(?=\r\n)/

这是相同的 JS 代码。

    var myString = "Location: Village 1 \r\n\r\nFeatures:  A multitude of offerings, including entrees, hot meals, wood-fired pizza, salad bar, grill items, made-to-order deli sandwiches & wraps, convenience items and much more\r\n\r\nPayment accepted: cash, Watcard  \r\n\r\nThis is a great place to meet your friends! The aroma of fresh baked breads and pastries from our in-house UW Bakery will surely make you take a deep breath. Mudie’s offers a large selection of vegetarian foods, grab n’ go items, salad bar, grill items, made-to-order deli sandwiches and pitas, full breakfast, and convenience foods. A hot entrée item and side dishes are available every lunch and dinner hour.\r\n\r\nMeal hours for Mom's Counter*:\r\n\r\nBreakfast: 7:30 - 11:00 am\r\n\r\nLunch:11:30 am - 2:00 pm\r\n\r\nDinner: 4:30 - 8:00 pm \r\n\r\n*please note, these hours are subject to change without notice "; // I want "abc"

    var arr = /Location:\s*([^\v]*)\s*Features:\s*([^\v]*)Payment accepted:([^\r\n]*)/.exec(myString);

    console.log("Location --> "+arr[1]);
    console.log("Features --> "+arr[2]);
    console.log("Payment accepted --> "+arr[3]);

关于javascript - 如何提取正则表达式中由单词后跟分号分隔的句子?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53896910/

相关文章:

javascript - 用于单页 Web 应用程序的 New Relic 真实用户监控

javascript - 表单不将数据保存到 mySQL 表中

python - 正则表达式清理网页抓取的文本

java - 如何使用正则表达式删除元音多于辅音的单词

javascript - 需要找到源路径正则表达式

javascript - 当我们确定对象已创建时如何获取对象的属性

javascript - 删除功能未按预期删除

javascript - pickadate.js 根据第一个输入设置焦点

python - 可以用正则表达式匹配字符重复吗?如何?

javascript - 使用正则表达式验证 Javascript 上的货币不起作用