How to check string in current URL with javascript?

This question already has an answer here:

  • Get current URL in JavaScript? 29 answers

  • what about this? The regular expression defined at the first line is tested against the URL:

    var myRe = //series//; //regular expression to use
    if(myRe.test(window.location.href)){
        alert("matching");
    }else{
        alert("not matching");
    }
    

    if you want to test for the whole URL (and really want to use a regular expression for that), you could replace the first line with

    var myRe = /^http://mywebsite/series/2545-abc$/;
    

    If you remove the dollar sign at the end, modifications at the end of the URL are accepted as well (eg http://mywebsite/series/2545-abc/foo.html )

    链接地址: http://www.djcxy.com/p/70240.html

    上一篇: URL的最后一部分

    下一篇: 如何检查当前URL中的字符串与JavaScript?