【问题标题】:Javascript - using match to find two words and return all text between them (in spite of line breaks)Javascript - 使用匹配查找两个单词并返回它们之间的所有文本(尽管有换行符)
【发布时间】:2015-02-27 15:14:38
【问题描述】:

在编程方面,尤其是 javascript,我是一个纯粹的业余爱好者,但我正在尝试解决一个问题,但我没有任何运气通过广泛的 Google 搜索找到解决方案。

我正在编写一个简短的脚本来获取从始终具有一致格式/布局但结果不同的网站复制的文本块,并将其粘贴到大文本字段中。然后单击一个按钮,它会从文本中提取某些信息,并以重新格式化的方式呈现这些信息。

我让它在我想做的大部分工作中发挥作用,但是文本的一部分包含多个换行符,因此我的匹配不起作用。当这场比赛失败时,它也会打破其余的比赛。具体来说,我正在尝试从以下文本中的“Restaurant:”和“Cuisine:”这两个词之间提取餐厅名称、地址、城市和邮政编码以及电话号码:

客人姓名:John Doe 电话:(555) 555-5555 预订日期:2015 年 3 月 14 日星期六时间:下午 6:00 聚会人数:2 确认号码:1703515901 餐厅:Café Boulud 20 东 76 街 纽约,纽约 10021 (212) 772-2600 菜式:法式、美式 餐厅寄语:感谢您在 Cafe Boulud 进行预订。国际客人:请在上面的空白处留下您的电子邮件地址和完整的电话号码。此信息将用于确认您与我们的预订。我们的菜单提供广泛的点菜选择,灵感来自厨师 Daniel Boulud 的四大烹饪缪斯:传统、经典的法国美食; la saison,时令美食; le potager,菜园;还有 le voyage,世界美食的味道。

这是我写的。它适用于所有预订详情,但不适用于餐厅详情:

<HTML>

<HEAD>



<script>
    function getVal(){

        var val = document.getElementById('ta').value;

        reserved = val.match("Name: (.*) Phone:");
        date = val.match("Date: (.*) Time:");
        time = val.match("Time: (.*) Party");
        party = val.match("Size: (.*) Confirmation");
        confirmation = val.match("#: (.*) Restaurant:");
        restaurant = val.match("Restaurant: (.*) Cuisine:");
    }       


    function myFunction() {
        var myWindow = window.open("", "myWindow", "width=400, height=400");
        myWindow.document.write(restaurant[1]+"</p>"+"Reserved Under: "+reserved[1]+"</br>Date: "+
        date[1]+" at "+time[1]+"</br>Party Size: "+party[1]+"</br>Confirmation #: "+confirmation[1]

        );
    }


</script>

</HEAD>

<BODY>



</p>


<form>
    <textarea rows="30" cols="100" id="ta"></textarea></br>
    <button onclick="getVal(); myFunction();">Get Value</button>
</form>

</BODY>

</HTML>

【问题讨论】:

  • 看起来我复制的文本块没有保留换行符。在“Cafe Boulud”、“76th St.”、“10021”和“(212) 772-2600”之后有一个\n
  • @georg 你忘记了文本中Cuisine 前面的\n。
  • @Ninetainedo:是的,完成了。
  • 感谢您修复布局。

标签: javascript regex match


【解决方案1】:

如果你想匹配每一个字符,你必须使用[\s\S]* 而不是.*

在你的情况下,你的正则表达式应该是"Restaurant: (\s\S*)Cuisine:" 如果我没记错的话,“Cuisine”之前没有空格,因为有换行符。

演示:

Restaurant: ([\s\S]*)Cuisine:

Debuggex Demo

HTML 实例:

function getVal(){

  var val = document.getElementById('ta').value;

  reserved = val.match("Name: (.*) Phone:");
  date = val.match("Date: (.*) Time:");
  time = val.match("Time: (.*) Party");
  party = val.match("Size: (.*) Confirmation");
  confirmation = val.match("#: (.*) Restaurant:");
  restaurant = val.match(/Restaurant: ([\s\S]*)Cuisine:/);
}       

getVal();
document.getElementById('result').innerHTML = "Restaurant : " +restaurant[1]+"\n"+"Reserved Under: "+reserved[1]+"\nDate: "+
  date[1]+" at "+time[1]+"\nParty Size: "+party[1]+"\nConfirmation #: "+confirmation[1];
#ta, #result
{
  width:100%;
  height:200px;
}
<h2>Text to parse :</h2>
<textarea id="ta">
Guest Name: John Doe Phone: (555) 555-5555 Reservation Date: Saturday, March 14, 2015 Time: 6:00 PM Party Size: 2 Confirmation #: 1703515901 Restaurant: Café Boulud
20 East 76th St.
New York, NY 10021
(212) 772-2600
Cuisine: French, American Message from the Restaurant: Thank you for making reservations at Cafe Boulud. FOR INTERNATIONAL GUESTS: Please leave your email address and full phone number in the space above. This information will be used to confirm your reservation with us. Our menu offers a wide range of a la carte options inspired by Chef Daniel Boulud's four culinary muses: la tradition, classic French cuisine; la saison, seasonal delicacies; le potager, the vegetable garden; and le voyage, the flavors of world cuisines.
</textarea>
<hr>
<h2>Result :</h2>
<textarea id="result">
</textarea>

【讨论】:

  • 我尝试更改代码,但似乎没有找到匹配项并获取我需要的文本。当我查看 Debuggex Demo 时,它也显示不匹配。
  • @Dave 它实际上在 Debuggex 和我的浏览器中都能正常工作。也许是浏览器问题?你用的是哪一个?
  • 嗯,奇怪。我正在使用 Google Chrome 版本 40.0.2214.115 m。也许我在编辑文件时出错了。我将行更改为: restaurant = val.match("Restaurant: ([\s\S]*)Cuisine:");
  • 在 Debuggex 中地址不是橙色的? “Restaurant”和“Cuisine”是黄色的?
  • 是的,但它也说“结果:从黑色三角形滑块开始不匹配”。当我在我的文件中进行更改并对其进行测试时,我得到一个空白的弹出窗口(这似乎在匹配不起作用时发生)。
猜你喜欢
  • 2022-01-14
  • 2017-09-11
  • 2018-02-11
  • 1970-01-01
  • 1970-01-01
  • 2016-10-05
  • 1970-01-01
  • 2013-04-16
  • 2014-08-14
相关资源
最近更新 更多