Check if a JavaScript string is a URL

前端 未结 30 3453
野趣味
野趣味 2020-11-22 15:41

Is there a way in JavaScript to check if a string is a URL?

RegExes are excluded because the URL is most likely written like stackoverflow; that is to s

30条回答
  •  深忆病人
    2020-11-22 16:19

    I change the function to Match + make a change here with the slashes and its work: (http:// and https) both

    function isValidUrl(userInput) {
        var res = userInput.match(/(http(s)?:\/\/.)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/g);
        if(res == null)
           return false;
        else
           return true;
    }
    

提交回复
热议问题