bessy
bessy

Reputation: 83

Regex URL Expression match

I am making an expression for validation the url in a right format. Expression works but i want if the url go beyond .com then it should not match. I mean expression starts from HTTPS/HTTP ---> .com. It should not go beyond .com like https://www.google.com/abcdefgh...

Regex:

 "/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i"

Upvotes: 1

Views: 106

Answers (1)

Gurmanjot Singh
Gurmanjot Singh

Reputation: 10360

After some minor changes and adding the lookahead (?=.*?\.com$), your working regex would look like this:

\b(?:(?:https?|ftp):\/\/)?www\.(?=.*?\.com$)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]

(?=.*?\.com$) validates that the regex will match the url only if it has .com in the end

Click for Demo

Upvotes: 1

Related Questions