How to match text after a tag using jquery?
I am trying to reformat the following text using jquery:
<a href="3.3/">3.3/</a> 14-Apr-2011 14:43 -
<a href="3.4.2/">3.4.2/</a> 06-Jun-2011 11:20 -
<a href="3.8/">3.8/</a> 01-Jun-2011 16:27 -
I am wrapping the links with li tags no problem there, but trying to extract the date and get rid of the rest of the text 开发者_如何学Gocruft is hurting my brain. I have looked a the jQuery text selectors but nothing seems to be popping up for me suggestions?
In an ideal world I would have something like this at the end of the day:
<li><a href="3.3/">3.3/</a><br><p>14-Apr-2011 14:43</p></li>
Since you want to isolate definable patterns in your content I would use regular expressions to parse out the data you're looking for then reformat that data as needed. Here is a basic example:
$(document).ready(function () {
var input = $('#content').html(); // Get content from #content container
var regex = /^(<a.+<\/a>)\s+(\S+)/gm; // Setup regex to extract anchor & date
// Execute regex against input and return subpattern matches
while( result = regex.exec(input) )
{
// Add formatted output to <ul>
$('ul').append("<li>" + result[1] + "<br><p>" + result[2] + "</p></li>");
}
});
This example assumes that your input string is inside the element #content and that you want to append your newly created <li>
's to <ul>
, which may not be the case. However, both assumptions should be easy for you to adapt to your needs.
精彩评论