Skip to content Skip to sidebar Skip to footer

Javascript - Using Match To Find Two Words And Return All Text Between Them (in Spite Of Line Breaks)

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

Solution 1:

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:

Regular expression visualization

Debuggex Demo

HTML Live example :

functiongetVal(){

  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><textareaid="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><textareaid="result"></textarea>

Post a Comment for "Javascript - Using Match To Find Two Words And Return All Text Between Them (in Spite Of Line Breaks)"