1

My url will be like localhost:5557. And I will not allow http:// or https:// or ftp:// in my url.

Also I tried

^(?:!(ftp|http|https):\/\/)?([a-zA-Z0-9]+\.)?[a-zA-Z0-9][a-zA-Z0-9-]+\.[a-zA-Z]{2,6}?$

But I am getting false when adding the port number.

Any suggestions please !

1 Answer 1

1

Negative lookahead must be like (?!..) not (?:!...)

^(?!(ftp|https?):\/\/)([a-zA-Z0-9]+\.)?[a-zA-Z0-9][a-zA-Z0-9-]+\.[a-zA-Z]{2,6}(?::\d{4})?$
Sign up to request clarification or add additional context in comments.

7 Comments

Can I make .com portion as optional ? Because my url may be like localhost:7678 or localhost.com:67876
^(?!(ftp|https?):\/\/)([a-zA-Z0-9]+\.)?[a-zA-Z0-9][a-zA-Z0-9-]+(?:\.[a-zA-Z]{2,6}|:\d{4})?$
Hi Avinash, this is working for me. What i need now is, I need to accept www. in my regex. For ex: www.test.com, www.test.com:76676 like this. And also I am not going to accept http|https|ftp. Can you help me to upgrade this regex which accepts www. ?
try ^(?!(ftp|https?):\/\/)([a-zA-Z0-9]+\.)?[a-zA-Z0-9][a-zA-Z0-9-]+(\.[a-z]{2,6})?(:\d{4})?$, regex101.com/r/lD6gY2/3
But this not working for websites which may contain subdomains such as www.test.example.com www.test.example.com:75667 ^(?!(ftp|https?):\/\/)([a-zA-Z0-9]+\.)?([a-zA-Z0-9]+\.)?[a-zA-Z0-9][a-zA-Z0-9-]+(\.[a-z]{2,6})?(:\d{4})?$ Is this right for urls with sub domains ??
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.