10

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 best?

This reg exp should match following type of URLs:

http://api.example.com/...
http://preview.example.com/...
http://www.example.com/...
http://purhcase.example.com/...

Just simple rule, like http://{something}.example.com/{something} then should pass.

Thank you.

2
  • 3
    What regex you tried so far? Show us your code and let us help you from there Commented Sep 23, 2015 at 2:43
  • Your question isn't clear. Does http://www.example.com/www match but http://www.example.com/subdomain not? Patterns reign supreme in regex so better be clear. Some counter examples also help. Commented Sep 23, 2015 at 3:17

6 Answers 6

20

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

4
  • 5
    I was looking for a similar regEx and this works except for one scenario. If I have a string like example.coms it still matches. How can i modify the Regex so that it does not match any character except / after example.com. Valid examples: example.com/abc/ Invalid examples: example.comaaa example.com.us
    – Frenz
    Commented May 20, 2016 at 6:10
  • as @Frenz commented, this would also match against example.commmmmmmm, I have tried to add start char and end char, but it makes no match at all, anyone found a solution?
    – am05mhz
    Commented May 31, 2018 at 10:51
  • 1
    Also, it is passing: google.sdff/sdfdf.example.com and for new-example.com/hack.for.example.com which is bad.
    – Moksh
    Commented Nov 16, 2022 at 5:29
  • Good catch, @Moksh. Thanks! After submitting this solution, I came to fear that the question was posed to help a student with homework, rather than to solve a real-world problem -- so I'm not going to edit it to fix the issue you found. A student should be able to do that themself.
    – Joe DeRose
    Commented Nov 17, 2022 at 15:37
10

Use indexOf javascript API. :)

var url = 'http://api.example.com/api/url';

var testUrl = 'example.com';

if(url.indexOf(testUrl) !== -1) {
    console.log('URL passed the test');
} else{
    console.log('URL failed the test');
}

EDIT:

Why use indexOf instead of Regular Expression.

You see, what you have here for matching is a simple string (example.com) not a pattern. If you have a fixed string, then no need to introduce semantic complexity by checking for patterns.

Regular expressions are best suited for deciding if patterns are matched.

For example, if your requirement was something like the domain name should start with ex end with le and between start and end, it should contain alphanumeric characters out of which 4 characters must be upper case. This is the usecase where regular expression would prove beneficial.

You have simple problem so it's unnecessary to employ army of 1000 angels to convince someone who loves you. ;)

3
  • well, it even pass just example.com. What I need is exactly this one: http://{something}.example.com/{something} You see, there MUST BE http there. just vote +1 for your answer.
    – Codemole
    Commented Sep 23, 2015 at 11:00
  • Exxactly what I needed, but could think of the indeOf(). I've liked the thinking outside the box solution you provided here :)
    – MwamiTovi
    Commented Mar 31, 2021 at 18:28
  • 1
    It is unsafe if you don't trust the source. hackerwebsite.com/?hackfor=example.com
    – Mike
    Commented Oct 25, 2022 at 10:52
3

Use this:

/^[a-zA-Z0-9_.+-]+@(?:(?:[a-zA-Z0-9-]+\.)?[a-zA-Z]+\.)?
(domain|domain2)\.com$/g

To match the specific domain of your choice.

If you want to match only one domain then remove |domain2 from (domain|domain2) portion.

It will help you. https://www.regextester.com/94044

1

Not sure if this would work for your case, but it would probably be better to rely on the built in URL parser vs. using a regex.

var url  = document.createElement('a');
url.href = "http://www.example.com/thing";

You can then call those values using the given to you by the API

url.protocol // (http:)
url.host     // (www.example.com)
url.pathname // (/thing)

If that doesn't help you, something like this could work, but is likely too brittle:

var url     = "http://www.example.com/thing";
var matches = url.match(/:\/\/(.[^\/]+)(.*)/);

// matches would return something like
// ["://example.com/thing", "example.com", "/thing"]

These posts could also help:

https://stackoverflow.com/a/3213643/4954530

https://stackoverflow.com/a/6168370

Good luck out there!

0
1

There are cases where the domain you're looking for could actually be found in the query section but not in the domain section: https://www.google.com/q=www.example.com

This answer would treat that case better.
See this example on regex101.

0

As you you pointed you only need example.com (write domain then escaped period then com), so use it in regex.

Example

UPDATED

See the answer below

1
  • Yes. You're right. But I think the regex could even include the whole mentioned pattern like http://{something}.example.com/{something}.
    – SergeyAn
    Commented Sep 23, 2015 at 3:30

Not the answer you're looking for? Browse other questions tagged or ask your own question.