javascript Reg Exp to match specific domain name

前端 未结 6 2075
小鲜肉
小鲜肉 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 09:57

    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. ;)

    0 讨论(0)
  • 2021-01-12 10:06

    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 讨论(0)
  • 2021-01-12 10:09

    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

    0 讨论(0)
  • 2021-01-12 10:10

    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

    0 讨论(0)
  • 2021-01-12 10:14

    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 讨论(0)
  • 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

    0 讨论(0)
提交回复
热议问题