0

I need a simple JavaScript regex that will validate all of the following

http://example.com
https://example.com
example.com
*.example.com
https://example.com
http://*.example.com
https://*.example.com

I use .com here by way of example but it could well be something else

but not, for example, https://*example.com. I thought the following

/((https?:\/\/)?([*]\.)?[a-z0-9_-]+(\.[a-z0-9_-]+)+\.?)/gi

might do the trick. It does, almost. The trouble is it passes `http://*example.com'. I fail to see why this is happening. Admittedly, regexes are not quite my forte.

1 Answer 1

1

I think the following should work:

^((?:https?:\/\/)?(?:\*\.)?[\w.-]+)$

This works for the given test cases.

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

Comments

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.