简体   繁体   中英

Regexp javascript - url match with localhost

I'm trying to find a simple regexp for url validation, but not very good in regexing..

Currently I have such regexp: (/^https?:\/\/\w/).test(url)

So it's allowing to validate urls as http://localhost:8080 etc.

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&&&&&

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:\/\/\w+(\.\w+)*(:[0-9]+)?\/?$/

match URLs without path

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)

Port: (:[0-9]+)? to match or to not match a number starting with semicolon: :8000 (and it could be only one)

Path: \\/?(\\/[.\\w]*)* to match any alphanums with slashes and dots: /user/images/0001.jpg (until the end of the line)

( 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.)

* 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.

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..

For Example to match 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

not match:

  • 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

You can use this. This will allow localhost and live domain as well.

^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

let isValid;
try {
    new URL(endpoint); // Will throw if URL is invalid
    isValid = true;
} catch (err) {
    isValid = false;
}
^https?:\/\/(localhost:([0-9]+\.)+[a-zA-Z0-9]{1,6})?$

Will match the following cases :

Will NOT match the following cases :

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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