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

1

2 Answers 2

3

Just use a \S:

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

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

Sign up to request clarification or add additional context in comments.

Comments

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>
[email protected]

http://foo.co.uk/
http://regexr.com/foo.html?q=bard
https://mediatemple.net jhjhjhjhjhjh
</pre>

5 Comments

Because \b stops on a word boundery and . is a such a boundery.
isn't space considered as word boundary ?
Yes, but it stops earlier at the first dot.
https?:\/\/.*\..*\b ---- this is not stopping at . or space in regexr.com
Because there is no limit to .*, try to make it lazy .*?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.