0

The regular expression in isURL is not working for all cases.

False negatives (incorrectly returns false):

https://link.springer.com/article/10.1057/s41303-017-0039-0
https://www.academia.edu/12962582/Embodiment_Semiosis._Violi_P

False positives (incorrectly returns true:

3.5/5

Create a regexp which returns true for the following URLs:

http://nonssl.com
https://ssl.com
localhost:3000/
nohttp.com
withslash.com/
sub.domain.com
withpath.com/a/b

And returns false for the following:

e.g.

3.5/5

My current RegExp =

/^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w-]+)+[\w\-_~:/?#[\]\@!\$&'\(\)\*\+,;=]+$/i

Plus I found this RegExp = ^((ftp|http|https)://)?(www.)??(?!.(ftp|http|https|www.))??[a-zA-Z0-9_-]+ (.[a-zA-Z]+)+((/)[\w#]+)(/\w+\?[a-zA-Z0-9_]+=\w+(&[a-zA-Z0-9_]+=\w+)*)?$ but accept everthing except localhost:3000, http://localhost:3000/

1 Answers1

0

Here is an attempt

You can add test for pathname to be sure

`https://link.springer.com/article/10.1057/s41303-017-0039-0
https://www.academia.edu/12962582/Embodiment_Semiosis._Violi_P
http://nonssl.com
https://ssl.com
localhost:3000/
nohttp.com
withslash.com/
sub.domain.com
withpath.com/a/b
3.5/5`.split("\n")
  .forEach(txt => {
      let url = new URL(txt.indexOf("http") === 0 ? txt : "http://" + txt);
      let testHost = txt.indexOf("http") === 0 ? txt.split("//")[1].split("/")[0] : txt.split("/")[0];
      if (url.port) console.log("port found",testHost, url.hostname, url.port, testHost === url.hostname + ":" + url.port)
        else console.log(testHost, url.hostname, testHost === url.hostname)
      })
mplungjan
  • 169,008
  • 28
  • 173
  • 236