简体   繁体   English

正则表达式 javascript - url 与本地主机匹配

[英]Regexp javascript - url match with localhost

I'm trying to find a simple regexp for url validation, but not very good in regexing..我正在尝试为 url 验证找到一个简单的正则表达式,但在正则表达式方面不是很好..

Currently I have such regexp: (/^https?:\/\/\w/).test(url)目前我有这样的正则表达式: (/^https?:\/\/\w/).test(url)

So it's allowing to validate urls as http://localhost:8080 etc.所以它允许将 url 验证为http://localhost:8080等。

What I want to do is NOT to validate urls if they have some long special characters at the end like: http://dodo....... or http://dododo&&&&&我想要做的是验证 url 如果它们在末尾有一些长的特殊字符,例如: http://dodo.......http://dododo&&&&&

Could you help me?你可以帮帮我吗?

How about this?这个怎么样?

/^http:\/\/\w+(\.\w+)*(:[0-9]+)?\/?(\/[.\w]*)*$/

Will match: http://domain.com:port/path or just http://domain or http://domain:port将匹配: http://domain.com:port/path或只是http://domainhttp://domain:port

/^http:\/\/\w+(\.\w+)*(:[0-9]+)?\/?$/

match URLs without path匹配没有路径的 URL

Some explanations of regex blocks:正则表达式块的一些解释:

Domain: \\w+(\\.\\w+)* to match text with dots: localhost or www.yahoo.com (could be as long as Path or Port section begins)域: \\w+(\\.\\w+)*匹配www.yahoo.com文本: localhostwww.yahoo.com (只要路径端口部分开始即可)

Port: (:[0-9]+)?端口: (:[0-9]+)? to match or to not match a number starting with semicolon: :8000 (and it could be only one)匹配或不匹配以分号开头的数字:8000 (它可能只有一个)

Path: \\/?(\\/[.\\w]*)* to match any alphanums with slashes and dots: /user/images/0001.jpg (until the end of the line)路径: \\/?(\\/[.\\w]*)*匹配任何带有斜线和点的字母: /user/images/0001.jpg (直到行尾)

( path is very interesting part, now I did it to allow lone or adjacent dots, ie such expressions could be possible: /. or /./ or /.../ and etc. If you'd like to have dots in path like in domain section - without border or adjacent dots, then use \\/?(\\/\\w+(.\\w+)*)* regexp, similar to domain part.) 路径是非常有趣的部分,现在我这样做是为了允许单独或相邻的点,即这样的表达式可能是可能的: /././/.../等等。如果你想在路径中有点就像在部分 - 没有边框或相邻点,然后使用\\/?(\\/\\w+(.\\w+)*)*表达式,类似于部分。)

* UPDATED * * 更新 *

Also, if you would like to have (it is valid) - characters in your URL (or any other), you should simply expand character class for "URL text matching", ie \\w+ should become [\\-\\w]+ and so on.此外,如果您想在您的 URL(或任何其他)中有(它是有效的) -字符,您应该简单地扩展“URL 文本匹配”的字符类,即\\w+应该变成[\\-\\w]+和很快。

It depends on how complex you need the Regex to be.这取决于您需要正则表达式的复杂程度。 A simple way would be to just accept words (and the port/domain):一个简单的方法是只接受单词(和端口/域):

^https?:\/\/\w+(:[0-9]*)?(\.\w+)?$

Remember you need to use the + character to match one or more characters.请记住,您需要使用+字符来匹配一个或多个字符。

Of course, there are far better & more complicated solutions out there.当然,还有更好、更复杂的解决方案

If you want to match ABCD then you may leave the start part..如果你想匹配ABCD那么你可以离开开始部分..

For Example to match http://localhost:8080例如匹配http://localhost:8080

' just write ' 写就好了

/(localhost).

if you want to match specific thing then please focus the term that you want to search, not the starting and ending of sentence.如果您想匹配特定的东西,那么请关注您要搜索的术语,而不是句子的开头和结尾。

Regular expression is for searching the terms, until we have a rigid rule for the same.正则表达式用于搜索术语,直到我们有一个严格的规则。 :) :)

i hope this will do..我希望这会做..

^https?:\/\/localhost:[0-9]{1,5}\/([-a-zA-Z0-9()@:%_\+.~#?&\/=]*)

match:匹配:

  • https://localhost:65535/file-upload-svc/files/app?query=abc#next https://localhost:65535/file-upload-svc/files/app?query=abc#next

not match:不匹配:

  • https://localhost:775535/file-upload-svc/files/app?query=abc#next https://localhost:775535/file-upload-svc/files/app?query=abc#next

explanation解释

  • it can only be used for localhost它只能用于本地主机
  • it also check the value for port number since it should be less than 65535 but you probably need to add additional logic它还检查端口号的值,因为它应该小于 65535 但您可能需要添加额外的逻辑

You can use this.你可以用这个。 This will allow localhost and live domain as well.这也将允许 localhost 和 live 域。

^https?:\/\/\w+(\.\w+)*(:[0-9]+)?(\/.*)?$

I'm pretty late to the party but now you should consider validating your URL with the URL class. Avoid the headache of regex and rely on standard我来晚了,但现在你应该考虑用URL class 验证你的 URL。避免令人头疼的正则表达式并依赖标准

let isValid;
try {
    new URL(endpoint); // Will throw if URL is invalid
    isValid = true;
} catch (err) {
    isValid = false;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM