开发者

How to identify all URLs that contain a (domain) substring?

If I am correct, the follow开发者_运维知识库ing code will only match a URL that is exactly as presented.

However, what would it look like if you wanted to identify subdomains as well as urls that contain various different query strings - in other words, any address that contains this domain:

    var url = /test.com/

    if (window.location.href.match(url)){
        alert("match!");
    }


If you want this regex to match "test.com" you need to escape the "." and both of the "/" that means any character in regex syntax.

Escaped : \/test\.com\/

Take a look for here for more info


No, your pattern will actually match on all strings containing test.com.


The regular expresssion /test.com/ says to match for test[ANY CHARACTER]com anywhere in the string

Better to use example.com for example links. So I replaces test with example.

Some example matches could be

http://example.com
http://examplexcom.xyz
http://example!com.xyz
http://example.com?q=123
http://sub.example.com
http://fooexample.com
http://example.com/asdf/123
http://stackoverflow.com/?site=example.com


I think you need to use /g. /g enables "global" matching. When using the replace() method, specify this modifier to replace all matches, rather than only the first one:

var /test.com/g;


If you want to test if an URL is valid this is the one I use. Fairly complex, because it takes care also of numeric domain & a few other peculiarities :

var urlMatcher = /(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?/;

Takes care of parameters and anchors etc... dont ask me to explain the details pls.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜