如何检查数组中的某个字符串是否在URL/window.location.pathname中

Jay*_*Dee 3 javascript jquery

我有一个数组:

var checkURL = ['abc123', 'abc124', 'abc125'];
Run Code Online (Sandbox Code Playgroud)

如何检查window.location.pathname中是否存在数组中的一个字符串?

我个人知道我可以使用:

<script type="text/javascript">
    $(document).ready(function () {
        if(window.location.href.indexOf("abc123") > -1) {
           alert("your url contains the string abc123");
        }
    });
</script> 
Run Code Online (Sandbox Code Playgroud)

tym*_*eJV 7

使用for循环进行线性搜索.

$(document).ready(function () {
    var checkURL = ['abc123', 'abc124', 'abc125'];

    for (var i = 0; i < checkURL.length; i++) {
        if(window.location.href.indexOf(checkURL[i]) > -1) {
            alert("your url contains the string "+checkURL[i]);
        }
    }
});
Run Code Online (Sandbox Code Playgroud)