I need to get a date (just as a string) from the HTML below:
<div class="patNameAddress">
<strong>Lastname, Firstname</strong><br />
Stree Address<br />
City ST Zip<br />
<br />
EXP DATE:10-08-2011<br />
<div>
The date string is always in the mm-dd-yyyy format, and always preceded by EXP DATE:
How do I find this string on the page and return it to use as a variable?
var matches = text.match(/EXP DATE:(\d{2}-\d{2}-\d{4})/));
matches[1]
仅包含日期,而matches[0]
包含整个匹配项(即,它包括EXP DATE :)。
place the date in a span as such <span class='date'>10-08-2011</span>
then using jQuery you can retrieve it by using
$(function(){
alert($(".patNameAddress .date").text());
});
Using jQuery:
function findDates(){
var dates = [];
$('.patNameAddress').each(function(){
date_temp = $(this).html().match(/EXP DATE\:(\d{2}-\d{2}-\d{4})/);
if (date_temp.length) dates.push(date_temp[1])
});
return dates;
}
Bonus: Fiddle!
This gets the job done:
[].map.call( div.childNodes, function ( node ) {
return node.nodeType === 3 ? node.data : '';
}).join( '' ).match(/EXP DATE:(.+)/)[1].trim()
where div
is a reference to that DIV element.
Live demo: http://jsfiddle.net/2ahmy/1/
Note: this code will break if you run it on a DIV that doesn't have a "EXP DATE:" text in it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.