gpt4 book ai didi

Javascript - 使用匹配来查找两个单词并返回它们之间的所有文本(尽管有换行符)

转载 作者:行者123 更新时间:2023-12-03 10:41:18 24 4
gpt4 key购买 nike

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

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

我让它可以完成我想做的大部分工作,但文本的一部分包含多个换行符,因此我的匹配无法正常工作。当这场比赛失败时,其余的比赛也会中断。具体来说,我试图从下面的文本中的“餐厅:”和“美食:”之间提取餐厅名称、地址、城市和邮政编码以及电话号码:

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é Boulud20 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.

Here's what I have written. It works for all of the reservation details, but not the restaurant details:

<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>

最佳答案

如果您想匹配每个字符,则必须执行类似 [\s\S]* 而不是 .* 的操作。

就您而言,您的正则表达式应该是“餐厅:(\s\S*)Cuisine:”如果我没猜错的话,“Cuisine”之前没有空格,因为有一个换行符。

演示:

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

Regular expression visualization

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>

关于Javascript - 使用匹配来查找两个单词并返回它们之间的所有文本(尽管有换行符),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28768307/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com