Nies
Nies

Reputation: 3

Regex for validate a website URL

The following regex:

(http|https):\/\/[\w-]+(\.[\w-]+)+([\w.,@?^=%&:\/~+#-]*[\w@?^=%&\/~+#-])?

Is matched for the following:

https://something.com, 
https://www.something.com, 
http://something.com, 
http://www.something.com

But the following is should not be match which is matched for the current regex:

https://www.something, http://www.something

And also the following is should be matched which is not matched for the current regex:

something.com, www.something.com

And also it matched for any domain, what I want to do is only accept known domains (such as: .com, .net, .biz etc).

How can I improve the above regex?

Upvotes: 0

Views: 1947

Answers (1)

alsjflalasjf.dev
alsjflalasjf.dev

Reputation: 1689

(:?(?:https?:\/\/)?(?:www\.)?)?[-a-z0-9]+\.(?:com|gov|org|net|edu|biz)

https://regex101.com/r/bH7eL5/2

Upvotes: 2

Related Questions