0

I have this regex

/[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}([a-zA-Z0-9@%._~#?={}]+-*[a-zA-Z0-9@%._~#=\/;]+)+.(?!-)[a-z]{2,24}\b((?:\{[^{}]*}|[-a-zA-Z0-9@%_+.~?&#\/=:$\[\]])*)$/

Which I want to match with these urls to true since I'm getting false with my current regex:

"ftp://user@host.com:80/abc/": true
"ftp://host.com": true
"http://abcewe.com": true
"https://abcewe.com": true
"mailto:someone@example.com": true
"news:newsgroup-name": true
"telnet://user:password@host:100": true
"http://ab-c.com": true

What do I need to add to match these urls? Thank you very much for the help.

Note: I need the other codes so I cant remove them.

Note: Here is the full urls that I need to match. The rest aside above is already match with this regex, the above urls isnt.

{
  "http://goog": false,
  "http://googl": false,
  "http://google": false,
  "https://www.domains.com/#home": true,
  "https://www.domains.com/{firstname}_{lastname}": true,
  "https://www.domains.com/file[3].html": true,
  "https://www.domains.com/?name=fred;age=50": true,
  "https://www.domains.com/{name{age}}": false,
  "https://www.domains.com/?name=fred;": false,
  "http://www.google.com": true,
  "https://www.amazon.com": true,
  "http://joe:secret@www.liquidcrystalstudios.co.cc:8080/abc/": true,
  "http://www.abcde.com:80/abc/": true,
  "ftp://user@host.com:80/abc/": true,
  "http://www.abcde.com/abc/": true,
  "http://www.abcde.com/": true,
  "http://joe:secret@www.liquidcrystalstudios.co.cc": true,
  "http://jocce:seccccret@www.liquidcrystalstudios.co.cc": true,
  "ftp://:abc@host.com/": false,
  "ftp://foo:@host.com/": false,
  "ftp://host.com": true,
  "http://abcde.com": true,
  "https://abcde.com": true,
  "gopher://gopher.meulie.net/": true,
  "mailto:someone@example.com": true,
  "news:newsgroup-name": true,
  "nntp://wild.server.example/example.group.n%2Fa/12345": true,
  "telnet://user:password@host:100": true,
  "wais://100.64.0.0:5": true,
  "file://vms.host.edu/disk$user/my/notes/note12345.txt": true,
  "prospero://host.dom//pros/name": true,
  "abc://a.com": false,
  "://abc.com": false,
  "http//abc.com": false,
  "http:abc.com": false,
  "HTTP://www.ABCDW.com/ABC/": true,
  "http://www.亜蔭.com/亜蔭/": false,
  "http://www.弌傲僉.com/弌傲僉": false,
  "http://www.俱咡.com/俱咡": false,
  "http://www.丂㒒儈.com/丂㒒儈": false,
  "http://www.亜腕弌.com/亜腕弌": false,
  "http://www.アバ.com/ショアバ": false,
  "http://www.アバショ.com/ジウンカ": false,
  "http://174.129.0.77": true,
  "http://174.1a.a.77": false,
  "http://174.10.23.0": false,
  "http://0.10.23.7": false,
  "http://174.10.23.+7": false,
  "http://-174.10.23.7": false,
  "http://17-4.10.23.5": false,
  "http://1.0.0.1": true,
  "http://126.255.255.254": true,
  "http://100.25.36.14": true,
  "http://128.0.0.1": true,
  "http://191.255.255.254": true,
  "http://150.25.36.14": true,
  "http://192.0.0.1": true,
  "http://223.255.255.254": true,
  "http://200.25.36.14": true,
  "http://10.0.0.0": false,
  "http://10.1.2.3": true,
  "http://10.255.255.255": true,
  "http://172.16.0.0": false,
  "http://172.0.0.5": true,
  "http://172.31.255.255": true,
  "http://192.168.0.0": false,
  "http://192.168.4.4": true,
  "http://192.168.255.255": true,
  "http://126.255.255.255": true,
  "http://127.0.36.14": true,
  "http://127.112.0.1": true,
  "http://10.190.201.10:0?port=0": false,
  "http://10.190.201.10:1?port=1": true,
  "http://10.190.201.10:21?port=21": true,
  "http://10.190.201.10:80?port=80": true,
  "https://10.190.201.10:443?port=443": true,
  "http://10.190.201.10:65535?port=65535": true,
  "http://10.190.201.10:65535?port=65536": false,
  "http://gooo.gl/09GxbZc": true,
  "www.abc.com/miss-scheme": false,
  "http&://10.190.201.10/wrong-scheme": false,
  "http://test#url/wrong-hostname": false,
  "http://FE80:0000:0000:0000:0202:B3FF:FE1E:8329/hostname-ipv6": false,
  "http://10.190.201.10:10/path/1": false,
  "HTTP://www.ABC①㌔№∮.com/①㌔№∮C/": false,
  " http://  ": false,
  " http://zzz": false,
  " :// . . ": false,
  "https :// www. amazon. com": false,
  "ht tps://ww w.am azon.co m": false,
  "https://www.amazon.com ": false,
  "https:// www. amazon. com ": false,
  "htt ps://ww w.ama zon.c om ": false,
  "https ://www .amazon .com ": false,
  " https:// www. amazon. com": false,
  "http://-abc.com": false,
  "http://abc-.com": false,
  "http://ab-c.com": true,
  "http://.abc.com": false,
  "http://abc..com": false,
  "http://ab.c.com": true,
  "http://+abc.com": false,
  "http://ab+c.com": false,
  "http://abc+.com": false,
  "http://www.356.com": true,
  "http://www.35accd6.com": true,
  " http:/abc.com/acb": false,
  "http:/abc.com/acb ": false,
  "http:/a bc.com/a cb": false,
  "http://a.c": false,
  "http://.com": false,
  "http://www.com": false
}
Ayaka
  • 197
  • 1
  • 13
  • `I need the other codes so I cant remove them` - which codes are the other codes that you can't remove? – Bravo Jul 28 '21 at 00:31
  • `([a-zA-Z0-9@%._~#?={}]+-*[a-zA-Z0-9@%._~#=\/;]+)+.(?!-)[a-z]{2,24}\b((?:\{[^{}]*}|[-a-zA-Z0-9@%_+.~?\/=:$\[\]])*)$` this one – Ayaka Jul 28 '21 at 00:33
  • so, you can't remove that regex even though it doesn't do what you want? what are you actually asking for? – Bravo Jul 28 '21 at 00:34
  • 1
    @Bravo hmm i think he meant that he wanted to add more regex into the regex statement instead of removing or replacing some of them. – I_love_vegetables Jul 28 '21 at 00:35
  • oh, so `([a-zA-Z0-9@%._~#?={}]+-*[a-zA-Z0-9@%._~#=\/;]+)+.(?!-)[a-z]{2,24}\b((?:\{[^{}]*}|[-a-zA-Z0-9@%_+.~?\/=:$\[\]])*)$` can't change, but `[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}` can? – Bravo Jul 28 '21 at 00:37
  • 1
    First, good luck trying to create a regex that covers the [url standard](https://url.spec.whatwg.org/#parsing). Second, you can tray many options from [here](https://mathiasbynens.be/demo/url-regex). Third, this has been [asked](https://stackoverflow.com/questions/161738) [multiple](https://stackoverflow.com/questions/3809401) [times](https://stackoverflow.com/questions/8188645) – Rodrigo Rodrigues Jul 28 '21 at 00:38
  • It's not totally can't change. If there is need to add, its fine. But it shouldn't be remove since I have other urls that use that code to match. – Ayaka Jul 28 '21 at 00:38
  • to be honest, I can't see what `[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}` is trying to achieve ... 1 to 256 of the first set of characters, `.` , 1 to 6 of the second set of characters .... how does that even relate to URL's? never seen that – Bravo Jul 28 '21 at 00:44
  • I updated my question to post the full urls that I need to match. – Ayaka Jul 28 '21 at 00:53
  • Previously, I have this regex `/(?!-)(?!\.)(?!.*?\.\.)(?![a-zA-Z]+\-\.[a-zA-Z]+)(?![a-zA-Z]+\+[a-zA-Z]+\.[a-zA-Z]+)(?![a-zA-Z\#]+\/[a-zA-Z\-]+)(?![0-9]+\.[a-zA-Z0-9]+\.[a-zA-Z0-9]\.[a-zA-Z0-9]+)([a-zA-Z0-9@%._~#?={}]+-*[a-zA-Z0-9@%._~#=\/;]+)+.(?!-)[a-z]{2,24}\b((?:\{[^{}]*}|[-a-zA-Z0-9@%_+.~?\/=:$\[\]])*)$/` and the only mismatch is `"http://google": false` since my previous regex reads it as true. So I change my regex with the above. – Ayaka Jul 28 '21 at 00:56

0 Answers0