For the html below, I am trying to put a variable to just "oakbarrels" and a variable to just "Dec 25, 2011". I have already been able to get the "Dec 25, 2011" with regex but I cannot figure out how to get the rest. Basically I want to remove "By " and everything after the first " -":
<p class="review-rating">
By oakbarrels
- Dec 25, 2011
-
Something.com
</p>
<script>
var thedate = $('.review-rating').text().match(/\-\s([^\n]+)/)[1].trim();
var from = ???
</script>
var matches = $('.review-rating').text().match(/\s*By\s+(\w+)\s*-\s*([\w, ]+)/);
jsFiddle.
matches[1] will contain 'oakbarrels' and matches[2] will contain 'Dec 25, 2011', as per your example.
I also changed your html() to text(). It doesn't appear the HTML is relevant to matching the text.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With