0

I am trying to match URL using regex

https?:\/\/.*\..*

But unable to understand how to end the match if a space occurs after the URL.

For example in the below Image, for last match, i want it to end, before the space. But nothing seems to be working.

Can you also explain why adding \b (word boundary) at the end doesn't work ?

enter image description here

Ankur Marwaha
  • 1,613
  • 2
  • 15
  • 30

2 Answers2

3

Just use a \S:

https?:\/\/.*\.\S*

\S means: match everything that is not a space char (space, tab, delim..)

Neb
  • 2,270
  • 1
  • 12
  • 22
1

Look at the solution below using lazy and a non-capturing group for the last white space:

Look here for far better regex' What is the best regular expression to check if a string is a valid URL?

//well let us dive into this:

var matches = document.querySelector("pre").textContent.match(/https?:\/\/.*\..*/g);

console.log(matches);

/*
your regex does the following
search for http:// or https://
then you want to search for every character that is not a newline until you find a dot
after that you simply search for everything that is not a newline.


you need lazy and a non-capturing group, lazy is ? - (?=\s)
*/

var matches2 = document.querySelector("pre").textContent.match(/https?:\/\/.+?\..+?(?=\s)/g);
console.log(matches2);
<pre>
foo@demo.net

http://foo.co.uk/
http://regexr.com/foo.html?q=bard
https://mediatemple.net jhjhjhjhjhjh
</pre>
Mouser
  • 13,132
  • 3
  • 28
  • 54