Reputation: 17
I'm a pure amateur when it comes to programming, and javascript in particular, but I'm trying to work out a problem and I haven't had any luck finding a solution through extensive Google search.
I'm writing a short script to take a block of text that is copied from a website that always has a consistent format/layout, but different results, and paste it into large text field. Then you click a button and it extracts certain pieces of information from the text, and presents that information in a reformatted way.
I got it to work for a large part of what I want to do, but one section of the text includes multiple line breaks, and my match isn't working as a result. When this match fails it breaks the rest of it as well. Specifically I'm trying to pull the restaurant name, address, city & zip, and phone number from between the words "Restaurant:" and "Cuisine:" in the below text:
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.
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>
Upvotes: 0
Views: 220
Reputation: 3389
If you want to match EVERY single character, you have to do something like [\s\S]*
instead of .*
.
In your case, your regex should be "Restaurant: (\s\S*)Cuisine:"
If I get it right, there is no space before "Cuisine" because there is a line break.
Demo :
Restaurant: ([\s\S]*)Cuisine:
HTML Live example :
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>
Upvotes: 1