0

I am looking for a jquery code to find the url part from a tweet and alert it. I have this tweet wrapped inside a div with a class .tweet

some tweet text goes here https://t.co/CTyPa0sYmp

I tried Regx expression but I guess because of the uppercase in url, it can only find the beginning of it.

var searchText = jQuery('.tweet').text(),
urls = searchText.match(/\b(https)?(:\/\/)?(\S*)\.(\w{2,4})\b/i);
alert(urls);

but the code only alerts "https://t.co"

What exactly am I doing wrong here?

JSFIDDLE

Rahul
  • 311
  • 2
  • 3
  • 8

1 Answers1

1

Copied the following regex from here, should work in your case. And I'm marking this question as duplicate.

/\b((http|https)?:\/\/(?:www\.|(?!www))[^\s\.]+\.[^\s]{2,}|www\.[^\s]+\.[^\s]{2,})\b/ig
Community
  • 1
  • 1
Ammar Hasan
  • 2,436
  • 16
  • 22