javascript Reg Exp to match specific domain name

前端 未结 6 2119
小鲜肉
小鲜肉 2021-01-12 09:34

I have been trying to make a Reg Exp to match the URL with specific domain name.

So if i want to check if this url is from example.com what reg exp should be the bes

6条回答
  •  青春惊慌失措
    2021-01-12 10:19

    I think this is what you're looking for: (https?:\/\/(.+?\.)?example\.com(\/[A-Za-z0-9\-\._~:\/\?#\[\]@!$&'\(\)\*\+,;\=]*)?).

    It breaks down as follows:

    • https?:\/\/ to match http:// or https:// (you didn't mention https, but it seemed like a good idea).
    • (.+?\.)? to match anything before the first dot (I made it optional so that, for example, http://example.com/ would be found
    • example\.com (example.com, of course);
    • (\/[A-Za-z0-9\-\._~:\/\?#\[\]@!$&'\(\)\*\+,;\=]*)?): a slash followed by every acceptable character in a URL; I made this optional so that http://example.com (without the final slash) would be found.

    Example: https://regex101.com/r/kT8lP2/1

提交回复
热议问题