Ist there any better way to replace/wrap the h*tp://name.tld/request_url?parameter
or h*tps://name.tld/request_url?parameter
text in some certain html elements with an <a href>
.
Here is my code:
jQuery('#posts .post').each(function() {
elem = jQuery(this);
elem.html(
elem.text()
.replace(/(http?:\/\/?\S+)/g, "<a href='$1'>$1</a>")
);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="posts">
<div class="post">
Tweet text 1 http://google.com and other text.
</div>
<div class="post">
Tweet text 2 https://www.google.com and other text.
</div>
<div class="post">
Tweet text 3
</div>
<div class="post">
...
</div>
</div>
Any jQuery.wrap()
alternative would be nice, too.