-3

入力したURLが有効かどうかを確認したい。

ここに私のコードがあります

var regExpURL = /((http|https):\/\/(\w+:{0,1}\w*@)?(\S+)|)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
var websiteURL = $("#txtotherwebsite").val();
if(!regExpURL.test(websiteURL))
{
   $("#errorwebsite")[0].innerHTML = "Invalid website name.";
   $("#txtotherwebsite").removeClass("successTextBox").addClass("errorTextBox");
   return false;
}
else
{
    $("#errorwebsite")[0].innerHTML = "";
    $("#txtotherwebsite").removeClass("errorTextBox").addClass("successTextBox");
    return true;
}
return false;

しかし、私がこれを使用しているときは、他にのみ入ります。regExpURL.test(websiteURL) は常に true を返します。

4

1 に答える 1

-1

次の正規表現を使用します。

(?<Protocol>\w+):\/\/(?<Domain>[\w@][\w.:@]+)\/?[\w\.?=%&=\-@/$,]*

テストするには、http: //regexlib.com/RETester.aspxにアクセスしてください。

ソーステキストボックスに入れます:

http://www.usgs.gov
http://www.acl.lanl.gov/URI/archive/uri-archive.index.html
ftp://@host.com/
ftp://host.com/
ftp://foo:@host.com/
ftp://myname@host.dom/%2Fetc/motd
file://vms.host.edu/disk$user/my/notes/note12345.txt
prospero://host.dom//pros/name

正規表現テキストボックスに置く

(?<Protocol>\w+):\/\/(?<Domain>[\w@][\w.:@]+)\/?[\w\.?=%&=\-@/$,]*

上記のすべての URL に一致します

それがあなたを助けることを願っています

于 2013-10-12T08:36:32.410 に答える